Solved

Current identity value

Posted on 1998-08-26
9
262 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
ID: 1089765
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
ID: 1089766
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
ID: 1089767
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:kdimmock
ID: 1089768
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
 

Expert Comment

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

Try
dbcc checkident (tablename)

manish77
0
 

Author Comment

by:chunhsin
ID: 1089770
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
ID: 1089771
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
ID: 1089772
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
ID: 1089773
use @@IDENTITY  global variable to get the last identity value
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

685 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