create procedure

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

Previously granted privileges on a replaced procedure are preserved.

Syntax

Changes are in bold.

create [or replace] procedure
             
    [owner.]procedure_name[;number]    
    [[(@parameter_name datatype [(length)
    (precision[,scale])]    
    [= default][output]…)]] 
[with {recompile | execute as {owner | caller}} ] 
as {SQL_statements | external name dll_name}   

Parameter Changes for create or replace procedure

Example

This example is based on a table of information about products, defined as:

create table Products (
    ProductID int,           
    ProductName varchar(30),           
    Discontinued varchar(10))       

create procedure ProductType 
    @product_ID int,                          
    @type char(10) output   
as   
declare @prod_name char(20)   
select @prod_name = ProductName, @type =           
    case @prod_name                  
        when ‘Tee Shirt’ then ‘Shirt’                  
        when ‘Sweatshirt’ then ‘Shirt’                  
        when ‘Baseball Cap’ then ‘Hat’                  
        when ‘Visor’ then ‘Hat’                  
        when ‘Shorts’ then ‘Shorts’                  
        else 'UNKNOWN'           
    end   
from Products   
where ProductID = @product_ID       

select object_id("ProductType")       
-----------       
425049519   

This next command replaces the ProductType procedure using the or replace clause. The parameters for Tee Shirt and Sweatshirt are updated, but the object ID of the procedure remains the same.

create or replace procedure ProductType 
    @product_ID int,                          
    @type char(10) output   
as   
declare @prod_name char(20)   
select @prod_name = ProductName, @type =           
    case @prod_name                  
        when ‘Tee Shirt’ then ‘ T Shirt’                  
        when ‘Sweatshirt’ then ‘Long Sleeve Shirt’                  
        when ‘Baseball Cap’ then ‘Hat’                  
        when ‘Visor’ then ‘Hat’                  
        when ‘Shorts’ then ‘Shorts’                  
        else 'UNKNOWN'           
    end   
from Products   
where ProductID = @product_ID       

select object_id("ProductType")       
-----------       
425049519   

Objects Dependent on Replaced Procedures

A procedure that calls a replaced procedure is recompiled when it executes. If replacing the procedure changed the number or type of parameters, the calling procedure must be replaced. You can run sp_depends on the replaced procedure to verify whether there are calling procedures that are affected by the changed definition.

Permission Changes for create or replace procedure

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

Changes for replacing a procedure are in bold.

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

Auditing Changes for create or replace procedure

Changes are in bold.

Event Audit Option Command or access audited Information in extrainfo
11 create create procedure
  • 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