Click here to Skip to main content
15,906,301 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Suppose I have Table1 which contains ID as primary key.
Now if I delete the entry say for ID 2 and 3.
Now the next ID which is going to be used is 4.
Question: How do I determine this ID value (4) before a new record is inserted into the table.

Edit: removed shouting.
Posted
Updated 22-Sep-11 20:40pm
v2

Quit similar thing we have discuss here.

sql query return primary key[^]
 
Share this answer
 
Use @@Identity. See here[^].
 
Share this answer
 
Comments
Toniyo Jackson 23-Sep-11 2:56am    
OP needs the next id value before inserting. See my answer
Try this.
SQL
SELECT IDENT_CURRENT('tablename') + IDENT_INCR('tablename')
 
Share this answer
 
v2
If you really need this than you will have to implement the functionality yourself.
After each insert into the table store the next auto number to be used, for this you can use the methods suggested in the other solutions.
Now when records are removed you can use your stored value to determine the next number to be used.
Please note that an auto number reset[^] might disturb this functionality.

But rethink why you need this functionality as it not very useful for an auto number.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900