Solved

Current identity value

Posted on 1998-08-26
9
258 Views
Last Modified: 2010-03-19
I have a table with a identity column. Sometime I need to delete all the data in the table. How can I get the current identity value (next identity value) with no data in the table? (I am using SQL Server 6.5)?
0
Comment
Question by:chunhsin
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 3

Expert Comment

by:gmoriak
Comment Utility
This was not easy.  I have never seen it documented and you're probably not supposed to know it exists.  But here's the answer.

It is stored inside (it's binary - you'll have to pull it out) the updatestamp column in the sysindexes table for the row with your table name.  

select updatestamp from sysindexes where name = 'mytable'

then you'll have to use bitwise operators to get the value, because this has the next value and the increment seed in a single column.
0
 

Author Comment

by:chunhsin
Comment Utility
In the SQL Server Books on line, the updatestamp is the count of change to row or page. When I delete som row in the table, the updatestamp increment too. I don´t know how can I do what you said!
0
 
LVL 5

Expert Comment

by:mayhew
Comment Utility
Out of curiosity, why would you need to know the next number if the table is empty?

You can start entering new data without knowing what the next number should be.

If you supply more information, maybe we can figure out a way around your problem.  :)

0
 
LVL 1

Expert Comment

by:kdimmock
Comment Utility
You might be better getting the value back after an INSERT through the @@IDENTITY variable rather than predicting it beforehand (what if some other user gets in first? - who wins?)
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Expert Comment

by:manish77
Comment Utility
This answer is really secret stuff but I am offering it to you free.

Try
dbcc checkident (tablename)

manish77
0
 

Author Comment

by:chunhsin
Comment Utility
Sorry for the delay.

My situation is the following:
  I have a application that insert user login information into SQL Server. Everyday, I download the user login information to a log file, and delete from SQL Server. When I start my application I need to calculate an offset to be sum with a control number, and the offset is evaluate from the currente identity value that need to be get using ODBC.

  I can´t  use "dbcc checkident" because this command may reset the current identity value.
0
 
LVL 3

Expert Comment

by:gmoriak
Comment Utility
Food for thought - When something is this difficult to resolve, the solution often is to rethink your process, not to find a force fit workaround.

0
 
LVL 5

Expert Comment

by:mayhew
Comment Utility
When you start your application, could you add a dummy row, select the id value, add 1 to it, delete the dummy row and then do the calc?

It's not the most elegant solution, but I think it would work.  :)

Either that, or you're going to have to store the ID number in another table and have an after insert trigger on the first table that updates that value in the second table.

Don
0
 
LVL 2

Accepted Solution

by:
Ezhil earned 100 total points
Comment Utility
use @@IDENTITY  global variable to get the last identity value
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

772 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

10 Experts available now in Live!

Get 1:1 Help Now