How to add “IF NOT EXISTS” to create trigger statement

后端 未结 4 811
无人及你
无人及你 2021-01-01 09:03

I am using sql server 2008 R2. More specifically, Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) Apr 2 2010 15:48:46 Copyright (c) Microsoft Corporation Stan

4条回答
  •  灰色年华
    2021-01-01 09:30

    Certain statements like CREATE TRIGGER needs to be the first in a batch (as in, group of statements separated by GO ).

    https://msdn.microsoft.com/en-us/library/ms175502.aspx

    Alternatively you could do this

    IF NOT EXISTS ( SELECT  *
                FROM    sys.objects
                WHERE   type = 'TR'
                        AND name = 'Insert_WithdrawalCodes' ) 
    BEGIN
        EXEC ('CREATE TRIGGER Insert_WithdrawalCodes ON ...');
    END;
    

提交回复
热议问题