'CREATE TRIGGER'必须是查询批处理中的第一个语句

时间:2017-06-30 09:29:23

标签: sql tsql sql-server-2012

我的脚本包含在begin transaction和commit transaction中。 我甚至不能使用Go inbetween。 在脚本中我想禁用一个触发器,然后想要创建另一个触发器 但是当我同时执行两个语句时,我得到了foll错误:

  

Msg 111,Level 15,State 1,Line 6'CREATE TRIGGER'必须是查询批次中的第一个语句。

我的代码是:


BEGIN TRY  
BEGIN TRANSACTION 

DISABLE TRIGGER [dbo].[trg_Beneficiary_After_Update] ON [dbo].[Beneficiary] 

CREATE TRIGGER [dbo].[trg_Beneficiary_After_Update1] ON [dbo].[Beneficiary] FOR UPDATE AS
BEGIN 
INSERT INTO [dbo].[Beneficiary_History]([Beneficiary_Id],[Customer_Id],[Beneficiary_Type],[Nick_Name],[Rib_Key],[Action_Flag],[Benef_No],[Original_Date],[Provider_Id],[Create_Date])
SELECT [Beneficiary_Id],[Customer_Id],[Beneficiary_Type],[Nick_Name],[Rib_Key],'U',[Benef_No],[Create_Date],[Provider_Id],GETDATE() FROM DELETE
PRINT 'AFTER Beneficiary Table UPDATE1 trigger fired.'  
END

UPDATE [dbo].[Beneficiary] SET [Benef_No] = REPLACE (Benef_No, +2120, ++212) WHERE Benef_No like '+2120%'

DISABLE TRIGGER [dbo].[trg_Beneficiary_After_Update1] ON [$(oltpdb)].[dbo].[Beneficiary] ; 

ENABLE TRIGGER [dbo].[trg_Beneficiary_After_Update] ON [$(oltpdb)].[dbo].[Beneficiary] 

end
COMMIT TRANSACTION                                                                     END TRY  
BEGIN CATCH  
     ROLLBACK TRANSACTION
END CATCH; 

2 个答案:

答案 0 :(得分:2)

@Priyanka,您可以使用动态SQL创建嵌入存储过程的触发器。使用分号将以批处理方式执行脚本。但是,我相信,在这个实例中创建触发器总是最好的做法。测试此脚本并让我知道它是否有效。

BEGIN TRY
    BEGIN TRANSACTION

    BEGIN
            ;

        DISABLE TRIGGER [dbo].[trg_Beneficiary_After_Update]
            ON [dbo].[Beneficiary];

        DECLARE @SQL VARCHAR(MAX);

        SET @SQL = 
            'CREATE TRIGGER [dbo].[trg_Beneficiary_After_Update1] ON [dbo].[Beneficiary] FOR UPDATE AS
               BEGIN 
            INSERT INTO [dbo].[Beneficiary_History]([Beneficiary_Id],[Customer_Id],[Beneficiary_Type],[Nick_Name],[Rib_Key],[Action_Flag],[Benef_No],[Original_Date],[Provider_Id],[Create_Date])
            SELECT [Beneficiary_Id],[Customer_Id],[Beneficiary_Type],[Nick_Name],[Rib_Key],''U'',[Benef_No],[Create_Date],[Provider_Id],GETDATE() FROM DELETED 
             PRINT ''AFTER Beneficiary Table UPDATE1 trigger fired'' END'

        EXEC (@SQL);

        UPDATE [dbo].[Beneficiary]
        SET [Benef_No] = REPLACE(Benef_No, + 2120, + + 212)
        WHERE Benef_No LIKE '+2120%';

        DISABLE TRIGGER [dbo].[trg_Beneficiary_After_Update1]
            ON [$(oltpdb)].[dbo].[Beneficiary];

        ENABLE TRIGGER [dbo].[trg_Beneficiary_After_Update]
            ON [$(oltpdb)].[dbo].[Beneficiary];
    END

    COMMIT TRANSACTION;
END TRY

BEGIN CATCH
    ROLLBACK TRANSACTION
END CATCH;

答案 1 :(得分:0)

插入“;”在终点线!

作为

BEGIN TRY  
BEGIN TRANSACTION 

DISABLE TRIGGER [dbo].[trg_Beneficiary_After_Update] ON [dbo].[Beneficiary]; 

CREATE TRIGGER [dbo].[trg_Beneficiary_After_Update1] ON [dbo].[Beneficiary] FOR UPDATE AS
BEGIN 
INSERT INTO [dbo].[Beneficiary_History]([Beneficiary_Id],[Customer_Id],[Beneficiary_Type],[Nick_Name],[Rib_Key],[Action_Flag],[Benef_No],[Original_Date],[Provider_Id],[Create_Date])
SELECT [Beneficiary_Id],[Customer_Id],[Beneficiary_Type],[Nick_Name],[Rib_Key],'U',[Benef_No],[Create_Date],[Provider_Id],GETDATE() FROM DELETE
PRINT 'AFTER Beneficiary Table UPDATE1 trigger fired.'  
END

UPDATE [dbo].[Beneficiary] SET [Benef_No] = REPLACE (Benef_No, +2120, ++212) WHERE Benef_No like '+2120%'

DISABLE TRIGGER [dbo].[trg_Beneficiary_After_Update1] ON [$(oltpdb)].[dbo].[Beneficiary] ; 

ENABLE TRIGGER [dbo].[trg_Beneficiary_After_Update] ON [$(oltpdb)].[dbo].[Beneficiary] ;

end
COMMIT TRANSACTION                                                                     END TRY  
BEGIN CATCH  
     ROLLBACK TRANSACTION
END CATCH;