Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sql2010
Starting Member
6 Posts |
Posted - 2010-04-06 : 14:44:33
|
how to write a delete trigger in sql server? Is this the right syntax/way to write a trigger that would delete the rows in the tableB for any deletes on the tableA based on the foreign key.CREATE TRIGGER trig_del_encountersON tableAFOR DELETE ASBEGIN DELETE from tableB where tableB.foreignkey= tableA.foreignkeyEND |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-04-06 : 16:48:29
|
In fact you can remove one of the joins tooDELETE BFROM tableB B--JOIN tableA A--ON A.ColumnZ = B.ColumnZJOIN deleted DON B.ColumnZ = D.ColumnZ |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
sql2010
Starting Member
6 Posts |
Posted - 2010-04-12 : 11:14:38
|
Thanks so much for your replies!! Is this the right trigger syntax?? CREATE TRIGGER trig_del_encountersON tableAFOR DELETE ASBEGIN DELETE *FROM tableBJOIN deleted dON b.foreignkey = d.foreignkeyEND |
|
|
sql2010
Starting Member
6 Posts |
Posted - 2010-04-12 : 11:26:09
|
I keep getting a syntax error when just trying to execute this trigger. |
|
|
sql2010
Starting Member
6 Posts |
Posted - 2010-04-12 : 11:35:27
|
This is the trigger that I am trying to execute:Any help is very much appreciated. Thanks in advance.CREATE TRIGGER test_trigON dbo.Table_1 FOR DELETE ASBEGINdelete from dbo.Table_2 a JOIN deleted d ON a.joincol = d.joincolEND |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|