Click here to Skip to main content
15,915,698 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

<tag>One of my textbox value should be next identity from Mssql.
How to do that could you please help?

Regards
Lancy
Posted
Comments
BobJanova 4-Nov-11 12:27pm    
Don't do it. By the time you come to submit that form, particularly in a web app, the value you had retrieved is quite possibly not the correct one any more.
Lancy.net 4-Nov-11 12:34pm    
Thanks i will try for next option

IDENT_CURRENT('tableName') (include the single quotes) returns the current value of the identity for the given table. This value should be the last-assigned identity value used in the table. In other words, you will have a row with this identity value already in the table, unless that row has been deleted. The identity value that will be assigned on the next INSERT will be IDENT_CURRENT('tableName') + IDENT_INCR('tableName').

I don't recommend relying on this, though. If you pre-determine the next identity value this way, you're bound to end up in a situation where another process makes the insert that actually gets that ID before yours does, so your process ends up using the wrong ID value.

It's much better to make your insert first (even if you don't have all the data yet), and use SCOPE_IDENTITY() to get the actual ID assigned.

You might wonder why SCOPE_IDENTITY() is better than IDENT_CURRENT('tableName'). As the name implies, the former will give you that most recent identity value assigned within your current scope (your batch, your stored proc, whatever), whereas the latter will give you the most recent identity assigned on the table, by anyone. Even though you might call IDENT_CURRENT right after 'INSERT, it's still possible that someone else's INSERT occurs in between, and IDENT_CURRENT will give you the identity value that resulted from their insert instead of yours, whereas SCOPE_IDENTITY will always give you yours.

eg

SELECT IDENT_CURRENT('table_name') as CurrentIdentity,
IDENT_INCR('table_name') as Increment,
IDENT_CURRENT('table_name') + IDENT_INCR('table_name') as NextIndentity
 
Share this answer
 
v3
Comments
Lancy.net 4-Nov-11 12:36pm    
Hi Arya thanks for your quick reply could you pls give a sample for SCOPE_IDENTITY()i feel this is better.
arya1685 4-Nov-11 12:44pm    
Check out this link

http://msdn.microsoft.com/en-us/library/ms190315.aspx
Lancy.net 5-Nov-11 3:04am    
Thanks
arya1685 5-Nov-11 8:48am    
Most wlcm
IDENT_Current,ScopeIdentity(),@@Identity . All of them return the current Identity value inserted. Increment the value and assign it to text proprty of text box.
 
Share this answer
 
v2

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