Hi,
 
Suppose you have table1 (auto increment PK) with a insert trigger that inserts a
row in table2 (auto increment PK).
 
When you insert a row in table1, the trigger will insert a row in the table2.
 
@@IDENTITY (after the table1 insert) will return the key generated for table2,
not table1 as expected/desired.
 
SCOPE_IDENTITY() (after the table1 insert) will return the key generated for
table1 because the insert statement to table1 runs in a context and the trigger
that inserts in table2 runs in another.
 
Just as Gilles explained.
 
Carlos Peix


  _____  

From: Clough, Samuel (USPC.PRG.Atlanta) [mailto:[EMAIL PROTECTED] 
Sent: Martes, 18 de Septiembre de 2007 07:19 a.m.
To: user-cs@ibatis.apache.org
Subject: RE: Do not use <selectKey> with SCOPE_IDENTITY and MSSQL?



Like the original email mentioned, wasn't there an issue with @@IDENTITY that
would make it somewhat unreliable?  I don't remember specifics, but there was a
reason I switched from it to SCOPE_IDENTITY a few years back.  Perhaps it was
just as issue in an older version of MS SQL.



From: Gilles Bayon [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, September 18, 2007 2:35 AM
To: user-cs@ibatis.apache.org
Subject: Re: Do not use <selectKey> with SCOPE_IDENTITY and MSSQL?



SCOPE_IDENTITY / @@IDENTITY
Returns the last identity value inserted into an identity column in the same
scope. 
A scope is a module: a stored procedure, trigger, function, or batch. Therefore,
two statements are in the same scope if they are in the same stored procedure,
function, or batch. 

SCOPE_IDENTITY and @@IDENTITY return the last identity values that are generated
in any table in the current session. 
However, SCOPE_IDENTITY returns values inserted only within the current scope;
@@IDENTITY is not limited to a specific scope. 

>From http://msdn2.microsoft.com/en-us/library/ms190315.aspx

So if you want to use the selectKey statement, use the @@IDENTITY.

If you want you can make an entry in the wiki
http://opensource.atlassian.com/confluence/oss/display/IBATIS/Home

-- 
Cheers,
Gilles

<a href="  <http://www.amazon.com/gp/registry/6JCP7AORB0LE>
http://www.amazon.com/gp/registry/6JCP7AORB0LE";>Wish List</a> 

  _____  

Princeton Retirement Group, Inc - Important Terms 

This E-mail is not intended for distribution to, or use by, any person or entity
in any location where such distribution or use would be contrary to law or
regulation, or which would subject Princeton Retirement Group, Inc. or any
affiliate to any registration requirement within such location. 

This E-mail may contain privileged or confidential information or may otherwise
be protected by work product immunity or other legal rules. No confidentiality
or privilege is waived or lost by any mistransmission. Access, copying or re-use
of information by non-intended or non-authorized recipients is prohibited. If
you are not an intended recipient of this E-mail, please notify the sender,
delete it and do not read, act upon, print, disclose, copy, retain or
redistribute any portion of this E-mail. 

The transmission and content of this E-mail cannot be guaranteed to be secure or
error-free. Therefore, we cannot represent that the information in this E-mail
is complete, accurate, uncorrupted, timely or free of viruses, and Princeton
Retirement Group, Inc. cannot accept any liability for E-mails that have been
altered in the course of delivery. Princeton Retirement Group, Inc. reserves the
right to monitor, review and retain all electronic communications, including
E-mail, traveling through its networks and systems (subject to and in accordance
with local laws). If any of your details are incorrect or if you no longer wish
to receive mailings such as this by E-mail please contact the sender by reply
E-mail. 


  _____  


Reply via email to