Stored Procedure

  • i have two tables T1 and T2. Both has the same schema. I am inserting a few records manually in T1 which has columns empname and empid.I need to create a stored procedure which should delete a particular record in T1 based on the parameter value in T1. Before the record is deleted, it should get copied into table T2 which has the same schema.

    How to achieve this? Pls give me the full stored procedure coding.

    It is quite urgent.

    Thanx in advance

    Regards

  • insert into T2 select * from T1 where ;

    delete from T1 where ;

    You can put it into T1 trigger too, so you don't have to manually call the SP:

    create trigger t1_xy on T1 for insert, update

    as

    insert into T2 select * from inserted where ;

    delete from T1 join inserted on ;

    Simple as that. Homework? exam question?

  • Actually, if you need to move the record from Table1 to Table2 on delete, you would use a trigger that fired ON DELETE to move the record.

    😎

  • oops, of course. I reread the original post again. I should be more careful.

  • Use the OUTPUT clause from the DELETE statement to insert the data as it gets removed.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Definately another way to do it. Haven't used it myself yet, but should look at when needed.

    😎

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply