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 |
mikeallenbrown
Yak Posting Veteran
72 Posts |
Posted - 2014-03-19 : 16:07:35
|
Trigger 1: When a new record is saved in table A insert Field1, Field2, field3, field4, and field5 into table B but only if it does NOT already exist in Table BTrigger 2: When a record in table A is deleted also delete from table B the row where field1 and field2 are the same between table A & BI'm struggling with how to do this ...still a SQL novice.Thank you.Mike Brown |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-03-19 : 16:57:16
|
Your insert trigger would be something like this. A similar thing for delete trigger, but you will be using the DELETED virtual table instead of the INSERTED virtual table.CREATE TRIGGER dbo.OnInsertOnYourTableAON TableA AFTER INSERTASINSERT INTO TableB(field1, field2, field3, field4, field5)SELECT field1, field2, field3, field4, field5FROM INSERTED iWHERE NOT EXISTS( SELECT * FROM TableB b WHERE i.field1 = b.field1 AND i.field2 = b.field2 AND i.field3 = b.field3 AND i.field4 = b.field4 AND i.field5 = b.field5)GO |
|
|
mikeallenbrown
Yak Posting Veteran
72 Posts |
Posted - 2014-03-19 : 18:37:37
|
Thank you!!Mike Brown |
|
|
mikeallenbrown
Yak Posting Veteran
72 Posts |
Posted - 2014-03-19 : 19:53:08
|
The INSERT trigger works perfectly. I can't get my DELETE trigger to work though ...I'm getting a syntax error.CREATE TRIGGER dbo.OnInsertOnYourTableAON TableA AFTER DELETEASDELETE FROM TableB(field1, field2, field3, field4, field5)SELECT field1, field2, field3, field4, field5FROM INSERTED iWHERE EXISTS( SELECT * FROM TableB b WHERE i.field1 = b.field1 AND i.field2 = b.field2 AND i.field3 = b.field3 AND i.field4 = b.field4 AND i.field5 = b.field5)GOMike Brown |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-03-20 : 10:48:16
|
Your syntax and logic has several issues. I was showing the insert trigger only as a template for how to write triggers rather than as something to be used for replicating the code as well. Delete trigger should be like this:CREATE TRIGGER dbo.OnDeleteOnYourTableAON TableA AFTER DELETEASDELETE bFROM TableB b INNER JOIN DELETED d ON d.field1 = b.field1 AND d.field2 = b.field2 AND d.field3 = b.field3 AND d.field4 = b.field4 AND d.field5 = b.field5; GO |
|
|
mikeallenbrown
Yak Posting Veteran
72 Posts |
Posted - 2014-03-20 : 11:20:32
|
Thank you James. I appreciate it.Mike Brown |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-03-20 : 13:03:36
|
You are very welcome.The logic that these triggers implement has the following flaw (or may be feature, perhaps). Suppose 1.I insert a row in the TableA with a specific set of values, for example field1 = 1, field2 = 2, field3 = 3, field4 = 4 and field5 = 5. This will insert one row into TableB with exactly those same values. 2. Insert a second row into TableB with exactly those same five values for field1 through field5. Now a second row will be inserted in TableA, but there will still be only one row in TableB.3. Delete one of the two rows in TableA. Now that will delete one row from TableA, leaving the second row intact, but it will also delete the one row in TableB. This will result in a row in TableA having no corresponding row in TableB.If this is an issue, you need to redesign the logic in the triggers. If this is the desired behavior, then you are all set. |
|
|
mikeallenbrown
Yak Posting Veteran
72 Posts |
Posted - 2014-03-20 : 13:22:36
|
Thanks again James. I'll take that into consideration.Mike Brown |
|
|
|
|
|
|
|