Solved

Sql indentity

Posted on 2010-11-24
5
336 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

832 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