get current identity of a table error???

  • Thread starter Thread starter haiwen
  • Start date Start date
H

haiwen

Hello, everyone:

I want to use an identity column as primary key(id) for a
table(table1), and I want to show the id number(current
identity + 1) before the data is inserted.

but the "SELECT IDENT_CURRENT('table1')" return 1 when
there is not data in the new table and after just insert
one row in the new table. So even the data will be
inserted correctly, there is a error information that id=2
when I am going to insert the first item.

Is there any way to know the table is brand new so that I
don't need to plus 1 to show the id number. or any better
way?

Thanks a lot,

haiwen
 
haiwen said:
Hello, everyone:

I want to use an identity column as primary key(id) for a
table(table1), and I want to show the id number(current
identity + 1) before the data is inserted.

but the "SELECT IDENT_CURRENT('table1')" return 1 when
there is not data in the new table and after just insert
one row in the new table. So even the data will be
inserted correctly, there is a error information that id=2
when I am going to insert the first item.

Is there any way to know the table is brand new so that I
don't need to plus 1 to show the id number. or any better
way?

It doesn't matter because what you're proposing just won't work.

If you get the IDENT_CURRENT of the table, by the time you go to insert the
row, some other user may have gotten the IDENT_CURRENT and already inserted
a row with the same id. If you use an identity column there is just no way
to know what value will be inserted until after you insert the row.

David
 
Back
Top