Today I run this
select 'exec sp_refreshview N''['+table_schema+'].['+table_name+']'''
from information_schema.tables
where table_type = 'view'
This generates a lot of: exec sp_refreshview N'[SCHEMA].[TABLE]'. I then copy the result to the query editor window and run all those execs.
How do I do this all at once? I would like to have a stored procedure called something like dev.RefreshAllViews which I can execute to do this...
Check the system procedure
SP_ExecuteSQL
, which accepts a string and executes it.You could write a stored procedure that opens a cursor on the query above, generates the proper strings, and executes them.
If there is ever any danger of your views having the [] characters in their names you might want to look at the QUOTENAME function.
Or Also with a cursor