all groups > sql server (alternate) > march 2006 >
You're in the

sql server (alternate)

group:

IDENT_CURRENT problem


IDENT_CURRENT problem john
3/21/2006 8:03:10 AM
sql server (alternate):
Hi there
I have small problem with IDENT_CURRENT...
We have a table where PK is generated automatically.
What I want is to get that value and insert it to another,
corresponding table.
So here is what I am doing...
BEGIN TRAN
Insert Into table() --> PK is created.
SELECT IDENT_CURRENT('Table_Name') AS lastValue
COMMIT TRan

IT works fine, but there is a possibility to insert another record by
another user before I get my IDENT_CURRENT.
For instance:
BEGIN TRAN
Insert Into table() --> PK is created.
---->somebody else inserts another record into same table.
SELECT IDENT_CURRENT('Table_Name') AS lastValue
---->this is not the value that I generated....
COMMIT TRan
Re: IDENT_CURRENT problem figital
3/21/2006 9:08:43 AM
Take a look at @@IDENTITY and SCOPE_IDENTITY().

In this case, you will probably want to use the function
SCOPE_IDENTITY().
Re: IDENT_CURRENT problem john
3/21/2006 9:13:42 AM
Thanks!!!!!
Re: IDENT_CURRENT problem Tom Moreau
3/21/2006 11:17:27 AM
Use SCOPE_IDENTITY().

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
[quoted text, click to view]
Hi there
I have small problem with IDENT_CURRENT...
We have a table where PK is generated automatically.
What I want is to get that value and insert it to another,
corresponding table.
So here is what I am doing...
BEGIN TRAN
Insert Into table() --> PK is created.
SELECT IDENT_CURRENT('Table_Name') AS lastValue
COMMIT TRan

IT works fine, but there is a possibility to insert another record by
another user before I get my IDENT_CURRENT.
For instance:
BEGIN TRAN
Insert Into table() --> PK is created.
---->somebody else inserts another record into same table.
SELECT IDENT_CURRENT('Table_Name') AS lastValue
---->this is not the value that I generated....
COMMIT TRan
Re: IDENT_CURRENT problem Tom Moreau
3/21/2006 11:39:46 AM
.... only if there is no trigger on the table that itself inserts into a
table with an identity.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
[quoted text, click to view]

The proper way to do it is via @@IDENTITY - see BOL.

robert
Re: IDENT_CURRENT problem Robert Klemme
3/21/2006 5:34:02 PM
[quoted text, click to view]

The proper way to do it is via @@IDENTITY - see BOL.

Re: IDENT_CURRENT problem Robert Klemme
3/22/2006 12:00:00 AM
[quoted text, click to view]

Well, SCOPE_IDENTITY seems to be the solution. Since it's mentioned on
the same page in BOL I didn't bother to mention it.

Cheers

AddThis Social Bookmark Button