Solved

How to get the next value of the primary key as a identity

Posted on 2006-10-24
7
353 Views
Last Modified: 2008-03-10
Sometime, I may delete the last recorder, but still want to get the next value
0
Comment
Question by:turbot_yu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
7 Comments
 
LVL 10

Assisted Solution

by:AaronAbend
AaronAbend earned 450 total points
ID: 17800833
there is no way to do this.  You really do not want to care what your primary key values are, which is why the "rules of relational database" include a rule that says that primary keys must not contain data. If you have questions about why let me know and I can tell you about systems I had to clean up because they violated this rule.

 
0
 
LVL 10

Assisted Solution

by:AaronAbend
AaronAbend earned 450 total points
ID: 17800879
I was wrong - it can be done...

DBCC CHECKIDENT
(
        'e1'  ,   RESEED  , 1    
)
 resets the identity on table e1 to 1 (next value will be 2)

I still recommend against it
0
 
LVL 10

Accepted Solution

by:
AaronAbend earned 450 total points
ID: 17800894
What happens if you insert 10 values and someone deletes record 4? You still have a gap in the values. Using a DBCC command (some of which are regarded as back door operations for emergencies) to go back, use ID 4, and reset back to 10, would be timeconsuming and would have to lock the table to prevent a primary key violation.

Note that if you are doing a transaction, you can use


SELECT top 1 IDENT_CURRENT('e') from e1

to get the current maximum value and write code to avoid "wasting" an id.

And I still think it's a bad idea!  


0
How our DevOps Teams Maximize Uptime

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

 
LVL 10

Assisted Solution

by:AaronAbend
AaronAbend earned 450 total points
ID: 17800897
sorry - I did not clarify my SQL...


SELECT top 1 IDENT_CURRENT('identity_columnname') from tablename

0
 

Author Comment

by:turbot_yu
ID: 17801078
when trying SELECT top 1 IDENT_CURRENT ('identity_groupid') from [group], it give null
when trying select max(groupid) from [group], it gives 6
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 50 total points
ID: 17801202
as said already by AaronAbend: DON'T

you can use the DBCC CHECKIDENT(yourtable, RESEED)  to reset the identity to the last value (so your next insert will take the next value), but don't try to specify a explicit value in normal circumstances.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

705 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