Created
April 17, 2017 18:01
-
-
Save ignas-sakalauskas/ad49012530f70b9742f7e40a0a7e8270 to your computer and use it in GitHub Desktop.
SQL RefreshViews
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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