1

I want to execute a SQL query that would return the current identity id of the row that was added to the table:

DECLARE @id int
DECLARE @tblOutput table (id int)

INSERT INTO tblStudent(Name, Family, age, test)
OUTPUT inserted.id into @tblOutput
VALUES('ashghar', 'farhadi', 321, 135)

SELECT @id = id from @tblOutput

Now my question is it the returned id for my current inserted row or is it the id of the last inserted row?

I mean can I trust it for using as foreign key?

4

1 回答 1

3

两者output inserted都会SCOPE_IDENTITY为您提供上一条语句刚刚插入的行中的 id。所以是的,您可以将其用作外键。

于 2016-01-23T08:50:19.750 回答