Creating a stored procedure if it does not already exist

后端 未结 9 1029
谎友^
谎友^ 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:56

    I like to use ALTER so I don't lose permissions and if you have a syntax error the old version still exists:

    BEGIN TRY
        --if procedure does not exist, create a simple version that the ALTER will replace.  if it does exist, the BEGIN CATCH will eliminate any error message or batch stoppage
        EXEC ('CREATE PROCEDURE AAAAAAAA AS DECLARE @A varchar(100); SET @A=ISNULL(OBJECT_NAME(@@PROCID), ''unknown'')+'' was not created!''; RAISERROR(@A,16,1);return 9999')
    END TRY BEGIN CATCH END CATCH
    GO
    
    ALTER PROCEDURE AAAAAAAA 
    (
         @ParamsHere varchar(10)
    )
    AS
    PRINT 'HERE IN '+(OBJECT_NAME(@@PROCID))
    GO
    

提交回复
热议问题