Creating a stored procedure if it does not already exist

后端 未结 9 1004
谎友^
谎友^ 2021-02-05 01:12

I want to check if a list of stored procedures exist. I want this all to be done in 1 script, one by one. So far I have this format:

USE [myDatabase]
GO

IF NO         


        
9条回答
  •  [愿得一人]
    2021-02-05 01:53

    I know that there's an accepted answer, but the answer does not address exactly what the original question asks, which is to CREATE the procedure if it does not exist. The following always works and has the benefit of not requiring dropping procedures which can be problematic if one is using sql authentication.

    USE [MyDataBase]
    GO
    
    IF OBJECT_ID('mySchema.myProc') IS NULL
    EXEC('CREATE PROCEDURE mySchema.myProc AS SET NOCOUNT ON;')
    GO
    
    ALTER PROCEDURE mySchema.myProc
        @DeclaredParmsGoHere    DataType
    
    AS 
       BEGIN
           DECLARE @AnyVariablesINeed    Their DataType
       SELECT myColumn FROM myTable WHERE myIndex = @IndexParm
    

提交回复
热议问题