Welcome Guest Search | Active Topics | Members | Log In | Register

Re: Using scope_identity Options · View
Jay
Posted: Friday, October 19, 2007 11:26:47 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 19/10/2007 23:26:47
Date: Sat, 20 Oct 2007 06:26:47 -0700

Nope, it is designed to give you the correct value. You can also use the
system variable @@IDENTITY.

Both 100% guaranteed to work correctly.

"JIM.H." <JIMH@discussions.microsoft.com> wrote in message
news:B54AEBF7-381C-4125-8F1D-038E87FEC1DD@microsoft.com...
> Using scope_identity
> I am using SQL2005 and I need to insert a record and return ID. I am using
> scope_identity() in the stored procedure to return the ID for the record
> just
> inserted.
> Do you see any problem with this when it comes to multi-user and
> multi-threaded environment.
>


Jay
Posted: Saturday, October 20, 2007 12:02:51 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 20/10/2007 12:02:51
Date: Sat, 20 Oct 2007 19:02:51 -0700

Excellent point, thank you.

I've only had a minor need to use an IDENTITY anyway, so it's only been a
single table that's had an IDENTITY when I've dealt with it.

@@IDENTITY, SCOPE_IDENTITY & IDENT_CURRENT('table') do indeed make sense and
BOL is very clear.

Thanks,
Jay

"Robert Klemme" <shortcutter@googlemail.com> wrote in message
news:5nuhbpFjqlttU1@mid.individual.net...
> On 20.10.2007 15:26, Jay wrote:
>> Nope, it is designed to give you the correct value. You can also use the
>> system variable @@IDENTITY.
>>
>> Both 100% guaranteed to work correctly.
>
> Well, both are guaranteed to work correctly (i.e. as specified). But
> there is a difference between the two which makes scope_identity the
> better choice. This is all written down in the documentation, e.g.
>
> http://msdn2.microsoft.com/en-us/library/ms190315.aspx
>
> Regards
>
> robert
>
>
> PS: Please do not top post.


Andrew J. Kelly
Posted: Saturday, October 20, 2007 1:36:02 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 20/10/2007 13:36:02
Date: Sat, 20 Oct 2007 20:36:02 -0700

It's not really a question of having more than one table with identity. If
you had a single table with an identity and you inserted a row and had a
trigger that inserted another row (unlikely not not impossible) then
@@Identity will return the 2nd inserts value not the original insert. There
is really no reason to use @@Identity anymore now that we have the others.

--
Andrew J. Kelly SQL MVP
Solid Quality Mentors


"Jay" <spam@nospam.org> wrote in message
news:eEz09Z4EIHA.5228@TK2MSFTNGP05.phx.gbl...
> Excellent point, thank you.
>
> I've only had a minor need to use an IDENTITY anyway, so it's only been a
> single table that's had an IDENTITY when I've dealt with it.
>
> @@IDENTITY, SCOPE_IDENTITY & IDENT_CURRENT('table') do indeed make sense
> and BOL is very clear.
>
> Thanks,
> Jay
>
> "Robert Klemme" <shortcutter@googlemail.com> wrote in message
> news:5nuhbpFjqlttU1@mid.individual.net...
>> On 20.10.2007 15:26, Jay wrote:
>>> Nope, it is designed to give you the correct value. You can also use the
>>> system variable @@IDENTITY.
>>>
>>> Both 100% guaranteed to work correctly.
>>
>> Well, both are guaranteed to work correctly (i.e. as specified). But
>> there is a difference between the two which makes scope_identity the
>> better choice. This is all written down in the documentation, e.g.
>>
>> http://msdn2.microsoft.com/en-us/library/ms190315.aspx
>>
>> Regards
>>
>> robert
>>
>>
>> PS: Please do not top post.
>
>

Robert Klemme
Posted: Saturday, October 20, 2007 6:23:49 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 20/10/2007 18:23:49
Date: Sat, 20 Oct 2007 16:23:49 +0200

On 20.10.2007 15:26, Jay wrote:
> Nope, it is designed to give you the correct value. You can also use the
> system variable @@IDENTITY.
>
> Both 100% guaranteed to work correctly.

Well, both are guaranteed to work correctly (i.e. as specified). But
there is a difference between the two which makes scope_identity the
better choice. This is all written down in the documentation, e.g.

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

Regards

robert


PS: Please do not top post.
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.1 (NET v2.0) - 9/10/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.057 seconds.