Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 next IDENTITY

Author  Topic 

ferrocuctt
Starting Member

2 Posts

Posted - 2002-11-15 : 22:53:50
is there a way to get the next IDENTITY of a table?

harshal_in
Aged Yak Warrior

633 Posts

Posted - 2002-11-15 : 23:30:28
quote:

is there a way to get the next IDENTITY of a table?




you can get current identity of the table by @@identity ,not sure @ the next identity.
harsh

Go to Top of Page

mohamedyousuff@yahoo.com
Starting Member

24 Posts

Posted - 2002-11-16 : 00:45:58

Say 'Employees' is the table for which the next IDENTITY has to be determined. For this use the IDENT_CURRENT function like

SELECT (IDENT_CURRENT('Employees') + 1 ) "NextValue"




quote:

is there a way to get the next IDENTITY of a table?





Go to Top of Page

harshal_in
Aged Yak Warrior

633 Posts

Posted - 2002-11-16 : 04:26:10
you can use the function ident_incr(table_name) which increments the current identity value by 1.
check BOL for details.
harshal.

Go to Top of Page

burbakei
Yak Posting Veteran

80 Posts

Posted - 2002-11-16 : 06:22:09
@@IDENTITY returns the last identity value generated by the statement
in the current session.

IDENT_INCR returns the increment value of identity column.

the problem with IDENT_CURRENT('TableName') + 1 is that if the table has no record
due to not addly any record physically,
or due to a truncate table (which resets the identity to the starting seed,
IDENT_CURRENT('TableName') returns starting seed.

so

DECLARE @NextIdent INT

SELECT @NextIdent = IDENT_CURRENT('TableName')
IF @NextIdent IS NOT NULL
BEGIN
IF @NextIdent = IDENT_SEED('TableName')
BEGIN
IF EXISTS(SELECT * FROM TableName)
SET @NextIdent = @NextIdent + IDENT_INCR('TableName')
END
ELSE
SET @NextIdent = @NextIdent + IDENT_INCR('TableName')
END


Go to Top of Page
   

- Advertisement -