SqlCommand which INSERTS and then SELECTs single value

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 ?
 
G

Guest

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
 
E

Edward Diener

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top