-- 刷新全部视图 IF NOT EXISTS(SELECT * FROM [DBO].SYSOBJECTS WHERE ID=OBJECT_ID('[DBO].[sp_RefreshAllView]') AND TYPE='P') BEGIN EXEC(' CREATE PROCEDURE [DBO].[sp_RefreshAllView] AS BEGIN DECLARE C_View CURSOR FOR SELECT DISTINCT [s].[name] + ''.'' + [o].[name] AS [ViewName] FROM SYS.OBJECTS [o] JOIN SYS.SCHEMAS [s] ON [o].[schema_id]=[s].[schema_id] WHERE [o].[type]=''V'' AND OBJECTPROPERTY([o].[object_id], ''IsSchemaBound'')<>1 AND OBJECTPROPERTY([o].[object_id], ''IsMsShipped'')<>1; DECLARE @view VARCHAR(60); OPEN C_View; FETCH NEXT FROM C_View INTO @view; WHILE @@fetch_status=0 BEGIN IF EXISTS(SELECT TABLE_SCHEMA + ''.'' + TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA + ''.'' + TABLE_NAME=@view) EXEC SP_REFRESHVIEW @view; FETCH NEXT FROM C_View INTO @view; END; CLOSE C_View; DEALLOCATE C_View; END; ') END; GO EXEC sp_RefreshAllView; GO