Capture the identity field assigned to the new inserted record

Capture the identity field assigned to the new inserted record

Post by Jean H » Sat, 15 Sep 2001 13:26:51



Hi,

How do I select the value of a identify field right after an insert
statement is executed ?

In my data_parent table, the rec_id is an identify field (auto number),
there is a data_child table which should contain the data_parent key
(rec_id), I need to capture the data_parent.rec_id right after I call the
insert in parent in order to setup the data_child with correct relationship.

Any suggestion is welcome.

Thanks.

Jean

 
 
 

Capture the identity field assigned to the new inserted record

Post by Ron Talmag » Sat, 15 Sep 2001 13:25:06


Jean,


IDENT_CURRENT('Tablename')

HTH,
Ron
--
Ron Talmage
SQL Server MVP


Quote:> Hi,

> How do I select the value of a identify field right after an insert
> statement is executed ?

> In my data_parent table, the rec_id is an identify field (auto number),
> there is a data_child table which should contain the data_parent key
> (rec_id), I need to capture the data_parent.rec_id right after I call the
> insert in parent in order to setup the data_child with correct
relationship.

> Any suggestion is welcome.

> Thanks.

> Jean


 
 
 

Capture the identity field assigned to the new inserted record

Post by Tibor Karasz » Sat, 15 Sep 2001 19:01:50


Ron,

Might I guess that you meant SCOPE_IDENTITY() instead of IDENT_CURRENT()?
SCOPE_IDENTITY() is multi-user safe, IDENT_CURRENT() is not...

--
Tibor Karaszi, SQL Server MVP
FAQ from Neil & others at: http://www.sqlserverfaq.com


> Jean,


> IDENT_CURRENT('Tablename')

> HTH,
> Ron
> --
> Ron Talmage
> SQL Server MVP



> > Hi,

> > How do I select the value of a identify field right after an insert
> > statement is executed ?

> > In my data_parent table, the rec_id is an identify field (auto number),
> > there is a data_child table which should contain the data_parent key
> > (rec_id), I need to capture the data_parent.rec_id right after I call the
> > insert in parent in order to setup the data_child with correct
> relationship.

> > Any suggestion is welcome.

> > Thanks.

> > Jean

 
 
 

Capture the identity field assigned to the new inserted record

Post by Ron Talmag » Tue, 18 Sep 2001 05:43:16


You're right, Tibor, SCOPE_IDENTITY() is the best way to capture your own
identity value right after an insert.

Thanks,
Ron