Um alle Views in einem Rutsch zu aktualisieren, hilft folgender Code:
DECLARE @view AS VARCHAR(255); DECLARE views_cursor CURSOR FOR SELECT TABLE_SCHEMA + '.' +TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'VIEW' AND OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'IsMsShipped') = 0 ORDER BY TABLE_SCHEMA,TABLE_NAME OPEN views_cursor FETCH NEXT FROM views_cursor INTO @view WHILE (@@FETCH_STATUS <> -1) BEGIN BEGIN TRY EXEC sp_refreshview @view; PRINT @view; END TRY BEGIN CATCH PRINT 'Error during refreshing view "' + @view + '".'; END CATCH; FETCH NEXT FROM views_cursor INTO @view END CLOSE views_cursor; DEALLOCATE views_cursor;
Funktioniert wunderbar.
In der Praxis z. B. hilfreich, wenn in den zugrunde liegenden Tabellen Spalten hinzugekommen sind oder entfernt wurden.