Refresh All Views in the database

DECLARE c CURSOR FOR
SELECT name
FROM sysobjects
WHERE type = ‘v’
AND uid = 1

OPEN c

DECLARE @ViewName VARCHAR(500)

FETCH next FROM c INTO @ViewName

WHILE @@FETCH_STATUS = 0
BEGIN

PRINT @viewName
EXEC Sp_refreshview @viewName

 

FETCH next FROM c INTO @viewName
END

CLOSE c

DEALLOCATE c

Advertisements