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

后端 未结 4 778
无人及你
无人及你 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

    The best way is to check for objects and drop them if they exist before you create them.

    Rather then not creating it at all if it exists, I would approach it the other way, drop it if exists and then create.

    Normally in long lenghty scripts if you want to update the definition of a trigger you would just simply add this at the end of that script and your trigger definition will be updated.

    So the approach should be create the object but drop it if it already exists rather then dont create it at all if it already exists

    IF OBJECT_ID ('[Insert_WithdrawalCodes] ', 'TR') IS NOT NULL
       DROP TRIGGER [Insert_WithdrawalCodes];
    GO
    
    CREATE TRIGGER .......
    

提交回复
热议问题