views:

45

answers:

3

i use the statement drop trigger if exist TRIGGER in sqlite but sql server doesnt like the if statement. (i guess exist is the offending word). I do this right next to my create trigger statement because i want to drop older triggers with the same name so i can replace it with this new one.

How do i do this in SQL server?

+4  A: 

in SQL Server Management Studio (and, I think in Query Analyzer) right-click the trigger in the explorer, and choose the Script-as option, choose 'Drop Trigger' in clipboard, and SSMS will create the T-SQL syntax for you to drop that trigger.

Sorry I haven't given you T-SQL you can copy and paste, but this way you'll know how to do it for next time.

cometbill
This is how I learned many of my scripting tactics. Realizing how poor the code generated by Enterprise Manager (and SSMS) really is is how I learned much of the rest.
Philip Kelley
unfortunately there are yet to be solved issue preventing me from running it on my machine.
acidzombie24
+1  A: 

You can check for the existence of a specific Trigger like so.

IF EXISTS
(
select name
from sys.objects
where type='TR' and name ='Trigger Name'
)
BEGIN

--Add your Trigger create code here

END
John Sansom
A: 

I'd use something like:

IF objectproperty(object_id('dbo.xxx'), 'isTrigger') = 1
    DROP PROCEDURE dbo.xxx
GO
CREATE TRIGGER dbo.xxx [etc]

replacing xxx with your trigger name (and dbo with the relevant schema, if necessary).

Alternatively, you could just use

ALTER TRIGGER dbo.xxx [etc]
Philip Kelley