Creating a stored procedure if it does not already exist

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

    IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spGetRailItems]') AND type in (N'P', N'PC'))
    BEGIN 
    execute ('
    CREATE PROCEDURE [dbo].[spGetRailItems]  
    AS  
    BEGIN  
    
    Declare @isLiftedBagsEnable bit=1;  
    select @isLiftedBagsEnable=cast(DataValu as bit) from setups where scope =''Rail Setting'' and dataName = ''isLiftedBagsEnable'';
    
    IF @isLiftedBagsEnable=1
    BEGIN
        IF EXISTS (SELECT * FROM ITEMCONFIG)
        BEGIN
            SELECT [Item],[Desc] FROM ProcData WHERE Item IN (SELECT Item FROM ItemConfig) ORDER BY [Desc]
        END
        ELSE
        BEGIN
            SELECT [Item],[Desc] FROM ProcData ORDER BY [Desc]
        END
    END
    ELSE
    BEGIN
        SELECT [Item],[Desc] FROM ProcData ORDER BY [Desc]
    END
    
    END
    
    ')
    END
    
    exec spGetRailItems;
    

提交回复
热议问题