Get last inserted ID in MSSQL

SQL server
To get the last generated ID of a query, in MySQL we use
mysql_insert_id

However this is not an option with MSSQL. An alternative is to use OUTPUT keyword as following:
INSERT INTO TableName (Field1,...,FieldN) OUTPUT INSERTED.ID VALUES(Value1,...ValueN)
And then execute this to get the scalar result.
in c#:
Invoke ExecuteScalar method of the SqlCommand.object.


Post a Comment

Previous Post Next Post