Refreshing Views

When you create a view, SQL Server stores metadata information describing the view, its columns, security, dependencies, and so on. Schema changes in underlying objects are not reflected in the view’s metadata information. After applying such schema changes, it’s a good practice to refresh the view’s metadata information using the sp_refreshview stored procedure so that the changes will be reflected in the view.

To demonstrate what can happen when you make schema changes and don’t refresh the view’s metadata information, first run the following code, which creates the table T1 and the view V1:

USE tempdb; GO IF OBJECT_ID('dbo.V1') IS NOT NULL DROP VIEW dbo.V1; GO IF OBJECT_ID('dbo.T1') IS NOT NULL DROP TABLE dbo.T1; GO CREATE TABLE ...

Get Inside Microsoft® SQL Server™ 2005 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.