How to find identity values in SQL Server
1] Identity_Scope(): Returns last identity value inserted in the table under current session
and scope i.e. it will return the last identity explicitly created on any table within current
session, but if an trigger is enabled on that table which in turn inserts an identity in
some other table Idenity_Scope() function won't trace the later inserted identity
2] @@identity: Returns last identity value inserted in the table under current session but
not restricted to current scope i.e. if an identity is explicitly created on a table which
has an trigger enabled on it which in turn inserts an identity in other table,
@@identity function will return the latest identity insert caused by trigger
3] Identity_Current('table_name'): Irrespective of the current session and scope,
Identity_Current('table_name') will return the last identity inserted in the table
mentioned,no matter what the current scope and session is. One point worth
mentioning here is table name always needs to be enclosed within single quotes ('')
How to explicitly reset identity values of a table
DBCC CHECKIDENT (tablename, RESEED, 0) command can be used to reset any
identity value within a table to zero, any identity inserts performed thereafter will
start with 1
for e.g.
DBCC CHECKIDENT (tablename, RESEED, 15) command will reset the identity value
to 15, so any identity getting inserted after that will start at 16
1] Identity_Scope(): Returns last identity value inserted in the table under current session
and scope i.e. it will return the last identity explicitly created on any table within current
session, but if an trigger is enabled on that table which in turn inserts an identity in
some other table Idenity_Scope() function won't trace the later inserted identity
2] @@identity: Returns last identity value inserted in the table under current session but
not restricted to current scope i.e. if an identity is explicitly created on a table which
has an trigger enabled on it which in turn inserts an identity in other table,
@@identity function will return the latest identity insert caused by trigger
3] Identity_Current('table_name'): Irrespective of the current session and scope,
Identity_Current('table_name') will return the last identity inserted in the table
mentioned,no matter what the current scope and session is. One point worth
mentioning here is table name always needs to be enclosed within single quotes ('')
How to explicitly reset identity values of a table
DBCC CHECKIDENT (tablename, RESEED, 0) command can be used to reset any
identity value within a table to zero, any identity inserts performed thereafter will
start with 1
for e.g.
DBCC CHECKIDENT (tablename, RESEED, 15) command will reset the identity value
to 15, so any identity getting inserted after that will start at 16
No comments:
Post a Comment