I have TableA and TableB into SQL Server 2008.
TableA have a trigger that fires after INSERT & UPDATE, On the trigger Im inserting into TableB so my question is how can i get the inserted ID from TableB? FROM INSERTED have the information of the record in TableA?
ALTER TRIGGER [dbo].[trg_up_move] ON [dbo].[TableA] AFTER INSERT, UPDATE
AS
BEGIN
DECLARE @idMovINS INT
DECLARE @stationINS char(8)
DECLARE @idWorkerINS INT
DECLARE @statusINS TINYINT
SELECT @idMovINS=id_mov FROM INSERTED
SELECT @stationINS=station_number FROM INSERTED
SELECT @idWorkerINS=id_worker FROM INSERTED
SELECT @statusINS=status_mov FROM INSERTED
-- CODE
-- MORE CODE
-- MUCH MORE CODE
IF @SOMEVAL='WISE DECISION'
BEGIN
DECLARE @idTableB INT
INSERT INTO TableB (FieldA,FieldB,FieldC)VALUES(@idWorkerINS,@stationINS,'More info')
--SET @idTableB=@@IDENTITY
--SET @idTableB=SCOPE_IDENTITY()
--MAKE SOME OTHER THINGS WITH @idTableB
END
END
If I send a hundred or thousand inserts to tableA on the same session. Can I use @@identity or scope_identity() (i go for the last)to get the b insert's identity? And how convinient and safe is to use one of this two functions taking in consideration each time the trigger is fired and the time to complete all the trigger's function