How do I use an INSERT statement’s OUTPUT clause to get the identity value?

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.

1 Answer
1

Leave a Comment