create view

The or replace clause allows you to replace a view's definition using create view.

Syntax

Changes are in bold.

create [or replace] view [owner.]view_name           
    [(column_name[, column_name]...)]            
    as            
    select [distinct] select_statement           
    [with check option]    

Parameter Changes for create or replace view

Examples

Example 1

This example is based on the view Current_Product_List which lists all active products from the table Products. The view is defined as:

create view Current_Product_List as
select ProductID, ProductName   
from Products  
where Discontinued = “No” 

select object_id("Current_Product_List")
-----------       
889051172   

This next command adds the Category column to Current_Product_list using the or replace clause. The object ID of the view remains the same:

create or replace view Current_Product_List as
select ProductID, ProductName, Category   
from Products  
where Discontinued = “No” 

select object_id("Current_Product_List")
-----------       
889051172  

Example 2

In this example, V1—a view that has dependent objects—is replaced:

create table T1(C1 int, C2 int)
create table T2(C1 int, C2 int) 
      
create view V1 as select * from T1   
create view V2 as select * from V1
       
create function foo1   
returns int   
as   
begin     
    declare @number int     
    select @number = C1 from V2   
end   
return @number       
select object_id("V1")       
-----------       
985051514 
create or replace V1 as select * from T2

select * from V2       

select dbo.foo1()       

select object_id("V1")       
-----------       
985051514 

The replaced version of V1 references T2 instead of T1. Both V2 and foo1 will be recompiled. select * from v2 recompiles V2, but not foo1, which is recompiled when the UDF is invoked.

Objects Dependent on Replaced Views

Views can be contained in other object definitions.
  • If the view that is replaced is contained in another view and the parent view is automatically recompiled when it is accessed.
  • If the number of columns in a view changes due to replacing, you may need to fix the definitions of other views and procedures that reference this view.
    In this situation, the owner has replaced V1 with different number of columns and column names. P must also be replaced.
    create view V1 as select C1 from T1 
    create procedure P as select * from V1
    create or replace V1 as select C1, C2 from T1 
    In this next situation, the owner has replaced V2 by removing C2 from the definition. When P is executed, an error is raised, as C2 is no longer part of V2 . Because of this, P must be replaced.
    create view V2 as select C1, C2 from T2 
    create procedure P as select C2 from V2 
    create or replace V2 as select C1 from T2  

    Before you replace a view, run sp_depends to determine if there are any stored procedures or parent views that depend on the view you are replacing. If such stored procedures or parent views exist, replace the stored procedures or parent views as necessary after replacing the view.

  • Instead of triggers defined on the view are dropped when the view is replaced
  • Any PRS that are dependent on the replaced view will require a full refresh to restore them to a usable state. You can neither refresh them, nor use them for query rewrite until they are recompiled.

Restrictions

If a view has permissions granted at the column level, then it cannot be replaced and error 2014 is raised. To replace the view, you must first revoke the permissions, or drop and re-create the view.

Permission Changes for create or replace view

Any user who impersonates the view owner through an alias or setuser cannot replace the view.

Changes for replacing a view are in bold.

Granular permissions enabled With granular permissions enabled, you must have the create view privilege. You must have the create any view privilege to run create view for other users. You must be the view owner to replace the view.
Granular permissions disabled With granular permissions disabled, you must be the database owner or have the create view privilege. You must be the view owner to replace the view.

Auditing Changes for create or replace view

Changes are in bold.

Event Audit Option Command or access audited Information in extrainfo
13 create create view
  • Roles – current active roles
  • Keywords or options – NULL
  • Previous value – NULL
  • Other information – NULL
  • Current value – NULL
  • Proxy information – original login name, if set proxy is in effect
  • or replace – for create or replace