Skip to content
Tags

SQL Refresh Views

April 2, 2012
DECLARE @Cursor CURSOR	
DECLARE @name VARCHAR(50)

SET @Cursor = CURSOR FORWARD_ONLY STATIC FOR
	SELECT  name
	FROM    sysobjects
	WHERE   [type] = 'V'

OPEN @Cursor
FETCH NEXT FROM @Cursor INTO @name

WHILE @@FETCH_STATUS = 0 
    BEGIN
        BEGIN TRY
            PRINT @name
            EXEC sp_refreshview @name
        END TRY
        BEGIN CATCH
            PRINT ERROR_MESSAGE()
            ROLLBACK TRANSACTION
        END CATCH

        FETCH NEXT FROM @Cursor INTO @name
    END

CLOSE @Cursor
DEALLOCATE @Cursor
Advertisements

From → SQL

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: