SqlCommand which INSERTS and then SELECTs single value

  • Thread starter Thread starter Edward Diener
  • Start date Start date
E

Edward Diener

I want to do an SqlCommand which INSERTs a row with an Identity column
and then SELECTs the single identity column with a SELECT ID from
MyTable where ID = SCOPE_IDENTITY(). The CommandText consists of the
INSERT followed by and separated by a ; from the SELECT. Does using the
ExecuteScalar call work correctly with such an Sql command ?
 
Edward,

The Select statement to retrieve the Identity column can just be:

SELECT SCOPE_IDENTITY()

You can append it to the INSERT statement separated by a semi-colon and use
ExecuteScalar to execute the INSERT and retrieve the new Identity.

Kerry Moorman
 
Kerry said:
Edward,

The Select statement to retrieve the Identity column can just be:

SELECT SCOPE_IDENTITY()

You can append it to the INSERT statement separated by a semi-colon and use
ExecuteScalar to execute the INSERT and retrieve the new Identity.

Thanks for the information and shorter version of retrieving the
identity I have just inserted.
 
Back
Top