Getting the new identity value from an Insert query

Getting the new identity value from an Insert query

Post by Boris Zakhari » Sat, 26 Jul 2003 02:45:17



Is it possible to return the newly created value in the identity column
after inserting one row into a table? Will it always be the maximum value in
the table?

Thanks,
Boris Zakharin

 
 
 

Getting the new identity value from an Insert query

Post by Vadim Rap » Sat, 26 Jul 2003 04:29:15


BZ> Is it possible to return the newly created value in the identity
BZ> column after inserting one row into a table? Will it always be the
BZ> maximum value in the table?

insert into table1 (cc) values('cc')  
select scope_identity()"

Vadim Rapp

----------------------------------------
Vadim Rapp Consulting
SQL, Access, VB Solutions
847-685-9073

 
 
 

Getting the new identity value from an Insert query

Post by Jacco Schalkwij » Sat, 26 Jul 2003 14:21:40


Hi Boris,

SCOPE_IDENTITY() will return the last identity value that has been inserted
in this scope.

This isn't at all guaranteed to be the maximum value.
1) The increment for the identity can be negative.
2) You can insert rows with different values in the identity column with SET
IDENTITY_INSERT <table name> ON
3) You can reset the seed (starting point) for the identity column with DBCC
CHECK_IDENT()

--
Jacco Schalkwijk MCDBA, MCSD, MCSE
Database Administrator
Eurostop Ltd.


Quote:> Is it possible to return the newly created value in the identity column
> after inserting one row into a table? Will it always be the maximum value
in
> the table?

> Thanks,
> Boris Zakharin