SQL 视图是否自动更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7755358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are Views automatically updated
提问by Hammad Khan
If I JOIN
or CROSS APPLY
two tables and create a VIEW
, will the view automatically gets updated when I update either of the two tables or I add records to either of them?
如果我JOIN
或CROSS APPLY
两个表并创建一个VIEW
,当我更新两个表中的任何一个或向其中一个添加记录时,视图是否会自动更新?
Will these new records show up in the VIEW
?
这些新记录会出现在VIEW
?
回答by Curt
Yes, they are updated, every time you use them.
是的,每次使用它们时,它们都会更新。
I think Microsoft sums up what a View is quite clearly:
我认为微软很清楚地总结了 View 的含义:
A view can be thought of as either a virtual table or a stored query.
可以将视图视为虚拟表或存储查询。
http://msdn.microsoft.com/en-us/library/aa214068%28v=sql.80%29.aspx
http://msdn.microsoft.com/en-us/library/aa214068%28v=sql.80%29.aspx
Views are not automatically cached.
视图不会自动缓存。
When you SELECT
from a view, the database has to run the query stored in the view to get the result set to use in your statement
当您SELECT
从视图中时,数据库必须运行存储在视图中的查询以获取要在您的语句中使用的结果集
The data you 'see' in a view, is not actually stored anywhere, and is generated from the tables on the fly.
您在视图中“看到”的数据实际上并未存储在任何地方,而是从表中动态生成的。
Because of this be careful running views which are very complex. Always take into account that the view will have to be executed before its result set is accessed.
因此,要小心运行非常复杂的视图。始终考虑到必须在访问其结果集之前执行视图。
回答by OTTA
A view is basically a stored query, it holds no data so no, it won't get updated when the tables it's built on are. However as soon as you reference the view the query it's based on will run, so you will see the changes made to the base tables.
视图基本上是一个存储查询,它不保存任何数据,所以当它构建的表是时它不会更新。但是,只要您引用视图,它所基于的查询就会运行,因此您将看到对基表所做的更改。
回答by OTTA
Yes, a view is a SELECT query against underlying tables/views. If you modify data in the underlying table(s), and if that range is included in the view definition then you will see the modified data.
是的,视图是针对基础表/视图的 SELECT 查询。如果您修改基础表中的数据,并且该范围包含在视图定义中,那么您将看到修改后的数据。
回答by yogen darji
Yes, records will be updated every time.
是的,每次都会更新记录。
But if you modify table definition.
Don't forget to refresh
view.
但是如果你修改表定义。不要忘记refresh
查看。
exec sp_refreshview @viewname
exec sp_refreshview @viewname
Don't use SELECT *
in view definition, instead use column name
不要SELECT *
在视图定义中使用,而是使用column name
回答by Programmerzzz
Just adding on to @Curt's Answer, if the update you made to underlying tables is adding or deleting Data, then the view is auto updated with the new data.
If you add or delete the columns form the underlying tables(basically the definition of the View ), then you need to run sp_RefreshView
stored procedure to reflect the new schema in your view.
只需添加@Curt 的答案,如果您对基础表所做的更新是添加或删除数据,则视图会自动更新为新数据。如果您添加或删除基础表中的列(基本上是 View 的定义),那么您需要运行sp_RefreshView
存储过程以在您的视图中反映新架构。
回答by ChristianProgrammer
Phtttt for what its worth 7 years later I went with what Neville Kuyt Oct 13 '11 at 14:07 recommended.
Phtttt 7 年后,我选择了 Neville Kuyt 于 2011 年 10 月 13 日 14:07 推荐的内容。
Some silly interview moments ago asked if data gets updated in the OG table if you update the view. I figured yea. So after interview I tested it. Created simple View from a some simple table. Wrote a simple update statement to update a column's value where PKId= whatever, then did selects against both the View and the OG table and column is updated in both result sets. Thus Yes you will update original table content from updating the view.
不久前一些愚蠢的采访询问如果更新视图,OG 表中的数据是否会更新。我想是的。所以面试后我测试了它。从一些简单的表创建简单的视图。编写了一个简单的更新语句来更新列的值,其中 PKId= 任何内容,然后针对视图和 OG 表进行选择,并在两个结果集中更新列。因此,是的,您将通过更新视图来更新原始表格内容。