Last active
September 28, 2015 22:37
-
-
Save ksouthworth/1506446 to your computer and use it in GitHub Desktop.
Refresh all SQL Views in database
This file contains hidden or 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
DECLARE @view AS VARCHAR(255) ; | |
DECLARE ListOfViews 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 ListOfViews | |
FETCH NEXT FROM ListOfViews | |
into @view | |
WHILE (@@FETCH_STATUS <> -1) | |
BEGIN FETCH NEXT FROM ListOfViews | |
INTO @view | |
BEGIN TRY | |
EXEC sp_refreshview @view; | |
PRINT @view; | |
END TRY | |
BEGIN CATCH | |
PRINT 'recorded error in this refreshView on : ' + @view; | |
END CATCH; | |
FETCH NEXT FROM ListOfViews | |
INTO @view | |
END | |
CLOSE ListOfViews; | |
DEALLOCATE ListOfViews; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment