SQL Server – Refresh/Rebuild all views

----------------------------------------------------------------------------
-- REFRESH ALL VIEWS
-----------------------------------------------------------------------------
SET NOCOUNT ON
DECLARE @SQL varchar(max) = ''
SELECT @SQL = @SQL + 'print ''Refreshing --> ' + sys.schemas.name + '.' + sys.objects.name + '''
EXEC sp_refreshview [' + sys.schemas.name + '.' + sys.objects.name + '];
'
FROM sys.objects
INNER JOIN sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id
WHERE type = 'V'
and sys.objects.name like 'vw_%' --< condition to select all views, may vary by your standards --SELECT @SQL EXEC(@SQL) go SELECT 'print ''Refreshing --> ' + sys.schemas.name + '.' + sys.objects.name + '''
EXEC sp_refreshview ' + sys.schemas.name + '.' + sys.objects.name + ';
'
FROM sys.objects
INNER JOIN sys.schemas ON sys.objects.schema_id = sys.schemas.schema_id
WHERE type = 'V'
and sys.objects.name like 'vw_%' --< condition to select all views, may vary by your standards