Re: SqlCommand which INSERTS and then SELECTs single value

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Kerry Moorman wrote:
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.


Kerry Moorman


"Edward Diener" wrote:

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 ?

.



Relevant Pages

  • Refresh not working on TableAdapter
    ... so I've read various articles on setting up a TableAdapter to ... retrieve a newly generated identity column value back into the dataset ... So then I name this new insert "InsertSingleRecord" (I've tried this ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: SSIS - Retrieve Identity from DB-DataFlow Destination(ID after ins
    ... > Is there an easy method to retrieve a identity value during/after a row is ... > 3) Create customer record with the AddressID (identity column) from ...
    (microsoft.public.sqlserver.dts)
  • Re: How Do I setup a RowChangedEvent in a Typed DataSet
    ... William R. Vaughn ... I have a strongly typed dataset and when I write the identity column to the ... I set it and retrieve it. ...
    (microsoft.public.dotnet.framework.adonet)
  • SQLCeCommandBuilder...
    ... I retrieve a dataset with 0-1 rows of a table. ... an identity column as primary key. ... Identity column is selected into the ... Dim dcConn As New SqlCeConnection ...
    (microsoft.public.dotnet.framework.compactframework)
  • SqlCommand which INSERTS and then SELECTs single value
    ... 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; ...
    (microsoft.public.dotnet.framework.adonet)