How to rebuild view in SQL Server 2008
I believe what you're looking for is
sp_refreshview [ @viewname = ] 'viewname'
Updates the metadata for the specifiednon-schema-bound view. Persistentmetadata for a view can becomeoutdated because of changes to theunderlying objects upon which the viewdepends.
In order to rebuild all views of a SQL Server database, you could use the following script:
DECLARE @view_name AS NVARCHAR(500);DECLARE views_cursor CURSOR FOR SELECT TABLE_SCHEMA + '.' +TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'VIEW' AND OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'IsMsShipped') = 0 ORDER BY TABLE_SCHEMA,TABLE_NAME OPEN views_cursor FETCH NEXT FROM views_cursor INTO @view_name WHILE (@@FETCH_STATUS <> -1) BEGIN BEGIN TRY EXEC sp_refreshview @view_name; PRINT @view_name; END TRY BEGIN CATCH PRINT 'Error during refreshing view "' + @view_name + '".'; END CATCH; FETCH NEXT FROM views_cursor INTO @view_name END CLOSE views_cursor; DEALLOCATE views_cursor;
This is a slightly modified version from this blog posting. It uses the sp_refreshview
stored procedure, too.
As well as Cory's answer, you could define it properly using schemabinding and the full column list.
CREATE VIEW MyViewWITH SCHEMABINDINGASSELECT col1, col2, col3, ..., colnFROM MyTableGO