Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm using sql server 2005 and have 2 tables in my database.
How can I write a trigger that can INSERT a record into table 2 BEFORE delete in table 1?
How can I do this?
I need this for VB.Net.
I've already done a connection with Sql server.
Posted
Updated 25-Nov-10 21:54pm
v2

Well, you need to write a trigger[^], keep in place in database.

Once thisw is done, using your connection to SQL Server, execute the query/stored procedure that deletes something from table1. Internally, trigger will handle this event and insert a record in table2 - after which delete would be executed in table1.
 
Share this answer
 
Comments
Dalek Dave 26-Nov-10 4:21am    
Good Answer.
Hi Neeil,
You can use trigger to solve your problem. Suppose you have a table named table1 whose structure is given below

table1
--------
id name age
1 xyz 26
2 abc 25

and you want to insert the deleted row into another table says table2(which has the same table structure as table1) before you delete from table1. So you can use the following trigger

SQL
create trigger trig_delete on table1 for delete
as
begin
insert into table2
select deleted.id,deleted.name, deleted.age from deleted
end


it will insert the deleted row into table "table2" while deleting the data from table "table1"

Hope this will solve your problem........ :-)
 
Share this answer
 

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