sql server - How to move Specific row in a table to another table when that row is updated -
for example have these 2 tables
employee(firstname, lastname, joindate, designationid,updatedate) employee_designation_history(employeeid, designationid)
if update employee table (changing destinationid
of employee
), need retrieve specific data of employee , save other table employee_designation_history
.
i'm guessing have column named employeeid
in first table well. can done trigger easy:
create trigger employee_update on employee update begin if update(designationid) -- if relevant column changed begin insert employee_designation_history (employeeid, designationid) select d.employeeid, d.designationid deleted d -- that's not typo, deleted correct pseudo table left join employee_designation_history edh on(d.employeeid = edh.employeeid , d.designationid = edh.designationid) edh.employeeid null end end
the reason selected data deleted i'm guessing want keep value of designationid before change. i've used left join on target table if have record same data not multiplied trigger.
i've used update() function make sure column in question indeed updated.
Comments
Post a Comment