Database Engine Stored Proc ...


SQL Server 2008 Books Online (November 2009)
sp_refreshview (Transact-SQL)

Updates the metadata for the specified non-schema-bound 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

Syntax

sp_refreshview [ @viewname = ] 'viewname' 
Arguments

[ @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.

Return Code Values

0 (success) or a nonzero number (failure)

Remarks

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.

Permissions

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.

Examples

A. Updating the metadata of a view

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

USE AdventureWorks;
GO
EXECUTE 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 AS so 
INNER JOIN sys.sql_expression_dependencies AS sed 
    ON so.object_id = sed.referencing_id 
WHERE so.type = 'V' AND sed.referenced_id = OBJECT_ID('Person.Contact');
See Also

Reference

Database Engine Stored Procedures (Transact-SQL)
System Stored Procedures (Transact-SQL)
sys.sql_expression_dependencies (Transact-SQL)
sp_refreshsqlmodule (Transact-SQL)

Other Resources

Understanding SQL Dependencies

Help and Information

Getting SQL Server 2008 Assistance
Tags :


Community Content

InTheMirror
Recompile all views

DECLARE @Table_Name varchar(120)
DECLARE Refresh_Views CURSOR FOR
SELECT Table_Name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'VIEW' AND TABLE_SCHEMA = 'dbo'
OPEN Refresh_Views
FETCH NEXT FROM Refresh_Views INTO @Table_Name
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC sp_refreshview @Table_Name
FETCH NEXT FROM Refresh_Views INTO @Table_Name
END
CLOSE Refresh_Views
DEALLOCATE Refresh_Views
Tags : contentbug

AaronJohal
Refresh all views for a table you just altered

-- Refreshing views for an object that you have just altered

USE

RelevantDB;

GO

SELECT

DISTINCT'EXEC sp_refreshview '''+ name +''''

FROM

sys.objectsAS so

INNER

JOINsys.sql_dependenciesAS sd

ON so.object_id= sd.object_id

WHERE

so.type='V'AND sd.referenced_major_id =OBJECT_ID('dbo.TableThatChanged');
Tags :

Page view tracker