I am new stored procedures and trying to write a procedure to duplicate user(by user_id) master detail data into a new user data. Here is my table structure. I will pass a target user_id and destination user_id to the stored procedure. Could you please let me know the how to achieve this functionality? I appreciate any help.
TABLE A
----------------
a_id int(Primary Key)
desc varchar(50)
user_id int
TABLE B
-----------------
b_id int(Primary Key)
a_id int(Foreign Key)
detail varchar(100)
CREATE PROCEDURE [duplicate_user_data]
@old_user_id int,
@new_user_id int
AS
BEGIN
INSERT INTO TABLEA
([desc]
,[user_id]
)
select desc, user_id from TABLEA where user_id = @old_user_id
END
You can use a self-join to match the old and new parent ids.