Creating a stored procedure if it does not already exist

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

    Just in case if you are using SQL server 2016, then there is a shorter version to check if the proc exist and then drop and recreate it

    USE [DATABASENAME]
    GO
    DROP PROCEDURE IF EXISTS 
    GO
    CREATE PROCEDURE 
    AS
    -- your script here
    END
    GO
    GRANT EXECUTE ON  TO 
    

    Source : https://blogs.msdn.microsoft.com/sqlserverstorageengine/2015/11/03/drop-if-exists-new-thing-in-sql-server-2016/

提交回复
热议问题