Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 4th, 2008, 11:35 AM
zjs
Guest
 
Posts: n/a
Default How can I get the identity generated by the SQL SERVER when I insert a record

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


 

Bookmarks

Thread Tools

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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles