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
begin try
PRINT '成功刷新视图: ' + @ViewName;
EXEC sp_refreshview @ViewName;
SET @i = @i + 1;
end try
begin catch
print ERROR_MESSAGE()
end catch
FETCH NEXT FROM #_cursor INTO @ViewName;
END;
CLOSE #_cursor;
DEALLOCATE #_cursor;
PRINT '刷新视图完成';
PRINT '共成功刷新' + CONVERT(VARCHAR(10), @i) + '个视图';