Copy row and all of its 'children'

后端 未结 2 1158
小鲜肉
小鲜肉 2021-01-21 06:07

I have a MySQL table called employees. There\'s another table called shifts that is tied to employees by the employeeId colum

2条回答
  •  感动是毒
    2021-01-21 06:33

    If you want to do this many times, or want to do it on a bulk level, create an sp that inserts a dupe entry for an employee and then inserts dupe entries for all his shifts.

    Something like:

    CREATE PROCEDURE sp_cloneEmployee
    @employeeId int
    AS
    INSERT INTO employees
    SELECT * FROM employees
    WHERE employeeId = @employeeId;
    
    INSERT INTO shifts
    SELECT * FROM shifts 
    WHERE employeeId = @employeeId;
    

提交回复
热议问题