Script to Refresh All Views in a SQL Server Database

Hi guys,

Views need to be refreshed if the underlying tables change at all. You shouldn’t have to run sp_refreshview for altering a view. Only for altering its underlying tables.
The following T-SQL script can be used to refresh all view inside a database:

Note that the script uses a cursor to go through all views of a database, it also handle possible errors giving view name, error number and error message.

Click on the following link if you want to know more about sp_refreshview.
Click on the following link if you want to know more about ERROR_NUMBER (Transact-SQL).
Click on the following link if you want to know more about ERROR_MESSAGE (Transact-SQL).

Thanks for reading.
Like and Share if you found it may be useful to someone you know!

Would you like to check the Portuguese version?
Click on the Brazilian flag -> Brazil

  1 comment for “Script to Refresh All Views in a SQL Server Database

  1. Cedrick
    02/08/2016 at 20:17

    Great Script. Nice touch to add the orphaned views that errored out

Leave a Reply

Your email address will not be published. Required fields are marked *

 
Translate »