sp_refreshview (Transact-SQL)
Updated:
14 April 2006
System Stored Procedures (Transact-SQL)
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.
Transact-SQL Syntax Conventions
A. Updating the metadata of a view
The following example refreshes the metadata for the view Sales.vIndividualCustomer.
USE AdventureWorks; GO 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;
GO
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')
Reference
Database Engine Stored Procedures (Transact-SQL)System Stored Procedures (Transact-SQL)
Help and Information
Getting SQL Server 2005 Assistance
Refresh all views example incomplete.
The example for the above is incomplete in that it does not take schema into account and produces literally sp_refreshview 'viewname' even if no view exists in dbo. To get the correct results, use:
SELECT 'EXEC sp_refreshview ''' + ss.name + '.' + so.name + ''''
FROM sys.objects so
INNER JOIN sys.sql_dependencies sd ON so.object_id = sd.object_id
INNER JOIN sys.schemas ss on ss.schema_id = so.schema_id
WHERE type = 'V'
AND sd.referenced_major_id = object_id('schema.TableName')
group by ss.name,so.name
SELECT 'EXEC sp_refreshview ''' + ss.name + '.' + so.name + ''''
FROM sys.objects so
INNER JOIN sys.sql_dependencies sd ON so.object_id = sd.object_id
INNER JOIN sys.schemas ss on ss.schema_id = so.schema_id
WHERE type = 'V'
AND sd.referenced_major_id = object_id('schema.TableName')
group by ss.name,so.name
- 8/23/2010
- Lukos