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
Phtttt for what its worth 7 years later I went with what Neville Kuyt Oct 13 '11 at 14:07 recommended.
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.
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.
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.
Yes, they are updated, every time you use them.
I think Microsoft sums up what a View is quite clearly:
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
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
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.
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.
Yes, records will be updated every time.
But if you modify table definition.
Don't forget to refresh
view.
exec sp_refreshview @viewname
Don't use SELECT *
in view definition, instead use column name