Solved

Sql indentity

Posted on 2010-11-24
5
327 Views
Last Modified: 2012-05-10
hey guys, i need an sql query to find the indentity, the hard part is when the table is empty it returning  value of 2

select IDENT_CURRENT('Customer') + IDENT_INCR ('Customer')

i am looking for the next indentity, if the table is empty return 1 and if 1 record is inserted the sql statement must return 2 and so on
0
Comment
Question by:JCWEBHOST
  • 2
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34204446
you should NOT try to "see" the next identity, at least not to display/use that.
only doing the insert, and then fetching the value using SCOPE_IDENTITY() is the correct (for multi.user apps anyhow) way to do
0
 

Author Comment

by:JCWEBHOST
ID: 34204507
give an exsample please
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34204567
Quote from books online: http://msdn.microsoft.com/en-us/library/ms175098.aspx
Be cautious about using IDENT_CURRENT to predict the next generated identity value. The actual generated value may be different from IDENT_CURRENT plus IDENT_INCR because of insertions performed by other sessions.

There is no other way I know of to correctly predict the next value.
The only way I know of that is not multi-user safe is to

insert a record, get scope_identity
delete record
fire ident_reseed to "return" the value used

This is, however, a terrible idea and as stated, not multi-user safe - you may end up reseeding wrongly.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34204804
after you run the INSERT (I presume you know how to do that), you run:

select scope_identity()

Open in new window

0
 

Author Closing Comment

by:JCWEBHOST
ID: 34228944
Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now