Insert to another table after inserting to a table
This is quite an often question asked by people.
There are 2 options. Create a stored procedure with 2 steps
something like
This is quite an often question asked by people.
There are 2 options. Create a stored procedure with 2 steps something like. It’ll insert the last value inserted to table A to table B
CREATE PROCEDURE InsertInsert AS BEGIN declare @id int Insert to table A select 'A', 'B' --take the last id inserted SET @id = @@identity GO Insert to table B Select Col1, Col2 from Table A where ID = @id END
or you can use trigger.
CREATE TRIGGER InsertToOtherTable ON TableSource AFTER INSERT AS INSERT INTO TableB SELECT Col1, Col2 FROM INSERTED
Advertisement
Categories: Transact SQL
@@identity, after insert, Create Trigger