PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET SqlCommand which INSERTS and then SELECTs single value

Reply

SqlCommand which INSERTS and then SELECTs single value

 
Thread Tools Rate Thread
Old 21-06-2006, 07:52 PM   #1
Edward Diener
Guest
 
Posts: n/a
Default 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 ; from the SELECT. Does using the
ExecuteScalar call work correctly with such an Sql command ?
  Reply With Quote
Old 21-06-2006, 09:33 PM   #2
=?Utf-8?B?S2VycnkgTW9vcm1hbg==?=
Guest
 
Posts: n/a
Default RE: SqlCommand which INSERTS and then SELECTs single value

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


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

  Reply With Quote
Old 22-06-2006, 02:02 PM   #3
Edward Diener
Guest
 
Posts: n/a
Default Re: SqlCommand which INSERTS and then SELECTs single value

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

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off