Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the interview I was asked a question:

Two tables are given I need to delete a data from one table and simultaneously add it in another table using Stored Procedure.

How to do it.
Posted

This can be done in numerous ways. One way would be to use the OUTPUT clause[^] and get the deleted rows into a table variable and then insert that into another table. Another possibility would be to utilize deleted table[^] inside a trigger. Of course a simple INSERT INTO ... SELECT structure before the deletion could also be an option.

However, to be precise, none of these are simultaneous.
 
Share this answer
 
v3
Comments
King Fisher 23-Feb-15 1:29am    
very nice my 5.
Wendelius 23-Feb-15 14:13pm    
Thank you !
Santosh K. Tripathi 23-Feb-15 2:11am    
5+
Wendelius 23-Feb-15 14:13pm    
Thanks !
The question was specific: 'delete a data from one table and simultaneously add it in another table !!! only through Stored Procedure !!!'. So you have to build a dynamic function which is able to conduct a set of operation (in a broader sense not only INSERT or DELETE) on multiple tables.

So you have to use SP, local variable, Transaction; I mean a dynamic process for this. Follow the link and "solution 4", it will be helpful. This is a basic example of UPDATE only. You can include INSERT and DELETE also.
Thank You.
How to make 1 column in two table and, if we change in one , another change automatically[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900