all groups > sql server (microsoft) > july 2006 >
You're in the

sql server (microsoft)

group:

determine a table's "true" identitiy value


determine a table's "true" identitiy value Brian D
7/26/2006 10:48:20 AM
sql server (microsoft):
I know that I can determine the identity value of a table using
IDENT_CURRENT('table_name') but if the table is new the IDENT_CURRENT
is misleading
(http://support.microsoft.com/default.aspx?scid=kb;en-us;835188).

Is there a way to determine if the table is new or if the identity
value has really been created?
Thanks

Brian
Re: determine a table's "true" identitiy value SQL
7/26/2006 11:34:04 AM
DBCC CHECKIDENT (TableName, NORESEED)


Change TableName to the real table name


Denis the SQL Menace
http://sqlservercode.blogspot.com/


[quoted text, click to view]
Re: determine a table's "true" identitiy value Brian D
7/27/2006 6:14:39 AM
Denis,

Thanks, but I should have elaborated more. I am doing this on an asp
pag where my SQL statement is:

SELECT nextID=(IDENT_CURRENT('images')+1)

If the table is empty nextID = 2, it should be 1. I am seeding it ae
(1,1). I tried (0,1) but that won't work for me for other reasons I
won't get into.

Any other ideas? Even if it is a way to see if the table as never been
written to.

Thank
Brian
Re: determine a table's "true" identitiy value Mike C#
7/27/2006 2:17:40 PM
Many people insert into the table first and use SCOPE_IDENTITY to retrieve
the identity value generated. Is that an option?

[quoted text, click to view]

Re: determine a table's "true" identitiy value Mike C#
7/27/2006 2:19:49 PM
Almost forgot - if you have more than one user using the method you
demonstrated to retrieve the next identity value simultaneously, I can see
some potential problems there. I.e., two users retrieving the same ID
number, etc.

[quoted text, click to view]

Re: determine a table's "true" identitiy value David Portas
7/28/2006 2:38:53 PM
[quoted text, click to view]

Don't try to predict the next IDENTITY value. For one thing it isn't
going to be reliable if you have more than one connection pushing
inserts to the table. For another, the sequence can have gaps so the
next value isn't necessarily the last value +1.

The safe and supported method is to retrieve the IDENTITY value AFTER
it is inserted. Use SCOPE_IDENTITY for that.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--
AddThis Social Bookmark Button