Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i recreate a trigger in SQL Server?

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?


2 Answers

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.

like image 63
cometbill Avatar answered May 12 '26 16:05

cometbill


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
like image 45
John Sansom Avatar answered May 12 '26 14:05

John Sansom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!