Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SQL RefreshViews
CREATE PROCEDURE [dbo].[Maintenance_RefreshViews]
AS
--Refresh the underlying metadata of all views
DECLARE @viewName AS VARCHAR(255)
DECLARE listOfViews CURSOR FOR
SELECT [name] FROM sysobjects WHERE xtype = 'V' ORDER BY [name]
OPEN listOfViews
FETCH NEXT FROM listOfViews into @viewName
WHILE (@@FETCH_STATUS <> -1)
BEGIN
PRINT @viewName
EXEC sp_refreshview @viewName
FETCH NEXT FROM listOfViews INTO @viewName
END
CLOSE listOfViews
DEALLOCATE listOfViews
GO
-- Usage: EXEC Maintenance_RefreshViews
-- https://ignas.me/tech/sql-refreshviews/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment