The textbook reason to run sp_refreshview: Persistent metadata for a view can become outdated because of changes to the underlying objects upon which the view depends.
You could turn this into a stored procedure, accepting a parameter for schema (@filterschema varchar(255)) and adding the where clause: where table_schema=@filterschema. Just some thoughts.
T-SQL with error catching for SQL Server 2005 (for example a column was removed from a table but is still referenced in a view):
DECLARE @vtr VARCHAR(1000)
DECLARE viewcur CURSOR FOR
SELECT table_schema + '.' + table_name FROM information_schema.views
order by table_schema, table_name
OPEN viewcur
FETCH NEXT FROM viewcur INTO @vtr
WHILE @@FETCH_STATUS <> -1
BEGIN
BEGIN TRY
EXEC sp_refreshview @vtr
END TRY
BEGIN CATCH
PRINT 'Errors found in view:' + @vtr + ', Error received: ' + ERROR_MESSAGE();
END CATCH;
FETCH NEXT FROM viewcur INTO @vtr
END
CLOSE viewcur
DEALLOCATE viewcur
No comments:
Post a Comment