How to get last inserted row into a database table from a named query

I recall this being possible but I haven't been able to make it work.

I have a named query

INSERT INTO dbo.RecipeLog(RecipeDataID, UpdatedBy)
VALUES (:RecipeDataID, :UpdatedBy)

SELECT SCOPE_IDENTITY() AS LastInsertedID:

I would expect this to return 143 or whatever the last row inserted is (there is an auto incrementing primary key on the table) but it instead returns 1 (the first row).

I think last time I put the SCOPE_IDENTITY statement inside a stored procedure is why it worked. Is there a way to make this work inside a named query?

Use the getKey argument when running the NQ. You should not be using SCOPE_IDENTITY() yourself.

3 Likes