Connecting Tech Pros Worldwide Help | Site Map

How can I get the identity generated by the SQL SERVER when I insert a record

  #1  
Old September 4th, 2008, 11:35 AM
zjs
Guest
 
Posts: n/a
for example
I can solve it in a single user.
_RecordsetPtr pR;
pConnection->Execute("Insert into TTest(nTest)
VALUES(100)"),&recordAffect,adCmdText);
pConnection->Execute("Select SCOPE_IDENTITY AS 'ID'
"),&recordAffect,adCmdText);
_variant_t vIndex = (LONG)0;
_variant_t vCount = pR->GetCollect("ID");

I get the identity in "vCount" successful.
but in multiusers. what should I do???any good advice?



  #2  
Old September 4th, 2008, 03:25 PM
Carl Daniel [VC++ MVP]
Guest
 
Posts: n/a

re: How can I get the identity generated by the SQL SERVER when I insert a record


zjs wrote:
Quote:
for example
I can solve it in a single user.
_RecordsetPtr pR;
pConnection->Execute("Insert into TTest(nTest)
VALUES(100)"),&recordAffect,adCmdText);
pConnection->Execute("Select SCOPE_IDENTITY AS 'ID'
"),&recordAffect,adCmdText);
_variant_t vIndex = (LONG)0;
_variant_t vCount = pR->GetCollect("ID");
>
I get the identity in "vCount" successful.
but in multiusers. what should I do???any good advice?
Not really a C++ question, but...

The solution is to perform both the insert and the retreival of
scope_identity in a single batch (i.e. present SQL server with both pieces
of text - the insert and the select - in a single call to Execute). You
could also make a stored proc in your database that does the insert followed
by the select and invoke that from your C++ code. Many database designers
would argue that that's the "correct" way.

See http://msdn.microsoft.com/en-us/library/ms190315.aspx for details on
SCOPE_IDENTITY.

-cd


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Server 2000 / Dataset /Relations Updating Mr Newbie answers 9 November 23rd, 2005 04:45 AM
UPDATE/INSERT to make One-to-Many table become One-to-One serge answers 14 July 23rd, 2005 10:24 AM
Is @@IDENTITY reliable? Edward answers 2 July 20th, 2005 03:19 AM
Help ASP; get last inserted value from one table, insert multiple rows in another table. PT answers 1 July 19th, 2005 03:26 PM