sp_refreshview (Transact-SQL)
Collapse the table of content
Expand the table of content

sp_refreshview (Transact-SQL)

Updated: 14 April 2006

Updates the metadata for the specified non-schemabound view. Persistent metadata for a view can become outdated because of changes to the underlying objects upon which the view depends.

Topic link icon Transact-SQL Syntax Conventions

sp_refreshview [ @viewname = ] 'viewname' 

[ @viewname = ] 'viewname'

Is the name of the view. viewname is nvarchar, with no default. viewname can be a multipart identifier, but can only refer to views in the current database.

0 (success) or a nonzero number (failure)

If a view is not created with schemabinding, sp_refreshview should be run when changes are made to the objects underlying the view that affect the definition of the view. Otherwise, the view might produce unexpected results when it is queried.

Requires ALTER permission on the view and REFERENCES permission on common language runtime (CLR) user-defined types and XML schema collections that are referenced by the view columns.

A. Updating the metadata of a view

The following example refreshes the metadata for the view Sales.vIndividualCustomer.

USE AdventureWorks;
exec sp_refreshview N'Sales.vIndividualCustomer';

B. Creating a script that updates all views that have dependencies on a changed object

Assume that the table Person.Contact was changed in a way that would affect the definition of any views that are created on it. The following example creates a script that refreshes the metadata for all views that have a dependency on table Person.Contact.

USE AdventureWorks;
SELECT DISTINCT 'EXEC sp_refreshview ''' + name + '''' 
FROM sys.objects so INNER JOIN sys.sql_dependencies sd 
ON so.object_id = sd.object_id 
WHERE type = 'V' 
AND sd.referenced_major_id = object_id('Person.Contact') 

Release History

14 April 2006

New content:
  • Added the Remarks section to clarify the reason for using sp_refreshview.
  • Added example B.
Changed content:
  • Updated example A.

Community Additions

© 2016 Microsoft