刷新SqlServer数据库中所有的视图

ALTER PROCEDURE sp_refallview
AS

--刷新所有视图
DECLARE @ViewName VARCHAR(MAX);
DECLARE @i INT;
SET @i = 0;
DECLARE #_cursor CURSOR
FOR
    SELECT  name
    FROM    sysobjects
    WHERE   type = 'V';

OPEN #_cursor;

FETCH NEXT FROM #_cursor INTO @ViewName;

WHILE @@fetch_status = 0
    BEGIN
        PRINT '成功刷新视图: ' + @ViewName; 
        EXEC sp_refreshview @ViewName;  
        SET @i = @i + 1; 
        FETCH NEXT FROM #_cursor INTO @ViewName;
    END;

CLOSE #_cursor;
DEALLOCATE #_cursor;
PRINT '刷新视图完成';
PRINT '共成功刷新' + CONVERT(VARCHAR(10), @i) + '个视图';

 

posted @ 2017-10-14 13:47  深南大道  阅读(2905)  评论(0编辑  收藏  举报