If I have an insert statement such as:
INSERT INTO MyTable
(
Name,
Address,
PhoneNo
)
VALUES
(
'Yatrix',
'1234 Address Stuff',
'1112223333'
)
How do I set @var INT
to the new row's identity value (called Id
) using the OUTPUT clause? I've seen samples of putting INSERTED.Name into table variables, for example, but I can't get it into a non-table variable.
I've tried OUPUT INSERTED.Id AS @var
, SET @var = INSERTED.Id
, but neither have worked.
OUTPUT
clause. – FeltonfeltsOUTPUT
clause writes to a table. It can be a table variable, temporary table, ... . – Clarisaclarise@@IDENTITY
because of trigger issues. There is a built-in function just for this case calledSCOPE_IDENTITY()
. Simply replace@@IDENTITY
withSCOPE_IDENTITY()
to get the truly inserted identity. – Medicate