October 1, 2018 at 7:46 am
Hi,
I would like to insert the records in Table 2 whenever the records delete in Table 1.
How to achieve this.
Please suggest.
Thank you !!
October 1, 2018 at 7:56 am
adisql - Monday, October 1, 2018 7:46 AMHi,I would like to insert the records in Table 2 whenever the records delete in Table 1.
How to achieve this.
Please suggest.Thank you !!
Trigger.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 1, 2018 at 8:01 am
If you are running a DELETE SQL statement you could use the OUTPUT statement to write the rows deleted to another table.
https://msdn.microsoft.com/en-us/library/ms177564.aspx
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
October 1, 2018 at 8:04 am
That'll work but, if you ever need to disable that particular functionality (especially temporarily), you'll need to do a code change rather than just disabling (or dropping) a trigger.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 1, 2018 at 8:28 am
Jeff Moden - Monday, October 1, 2018 8:04 AMThat'll work but, if you ever need to disable that particular functionality (especially temporarily), you'll need to do a code change rather than just disabling (or dropping) a trigger.
So dropping and then later adding back a trigger would be easier than temporarily commenting out a few lines of code? I guess I would go with commenting out the code.
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
October 1, 2018 at 8:38 am
below86 - Monday, October 1, 2018 8:28 AMJeff Moden - Monday, October 1, 2018 8:04 AMThat'll work but, if you ever need to disable that particular functionality (especially temporarily), you'll need to do a code change rather than just disabling (or dropping) a trigger.So dropping and then later adding back a trigger would be easier than temporarily commenting out a few lines of code? I guess I would go with commenting out the code.
The other good thing about a trigger is that it will insert the rows into the table even if you do a manual delete of the data not going through your code.
October 1, 2018 at 8:47 am
below86 - Monday, October 1, 2018 8:28 AMJeff Moden - Monday, October 1, 2018 8:04 AMThat'll work but, if you ever need to disable that particular functionality (especially temporarily), you'll need to do a code change rather than just disabling (or dropping) a trigger.So dropping and then later adding back a trigger would be easier than temporarily commenting out a few lines of code? I guess I would go with commenting out the code.
Yes.
Ideally, you would NOT drop the trigger if it were for temporary reasons. You'd just disable it with a single line of code. Depending on how things are setup for changes to prod, "commenting out a few lines of code" may require running through a "dev cycle"/change control, etc whereas temporarily disabling a trigger may not. If it's a permanent rather than temporary change, then you'd just drop the trigger. Either way, no actual code changes would be required, which would be especially painful if there were many places such DELETE code existed.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 1, 2018 at 9:02 am
Jonathan AC Roberts - Monday, October 1, 2018 8:38 AMbelow86 - Monday, October 1, 2018 8:28 AMJeff Moden - Monday, October 1, 2018 8:04 AMThat'll work but, if you ever need to disable that particular functionality (especially temporarily), you'll need to do a code change rather than just disabling (or dropping) a trigger.So dropping and then later adding back a trigger would be easier than temporarily commenting out a few lines of code? I guess I would go with commenting out the code.
The other good thing about a trigger is that it will insert the rows into the table even if you do a manual delete of the data not going through your code.
Exactly.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 2, 2018 at 12:50 pm
Jeff Moden - Monday, October 1, 2018 9:02 AMExactly.
I can see your points. But as with many things in SQL I will say "It depends". If you have SQL that does a delete in more than one job(why would you? I don't know) and you only want to temporarily stop one of the deletes from writing to this output table but keep any others going. It may be difficult figuring out the timing with the trigger, to be sure you ignore the one delete but catch any others.
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply