SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help with sequence
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

abhijitheie
Starting Member

2 Posts

Posted - 01/29/2013 :  02:36:59  Show Profile  Reply with Quote
Hi,

I have a column with name "CustomerNumber". I want to get increment the max value of this column by 1 and then return me the incremented number. I know we have to do this through stored procedures and sequence generators. but im not sure how exactly. please explain.

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/29/2013 :  02:46:15  Show Profile  Reply with Quote
the best way to deal with this is to make the column an identity one so that values will get incremented automatically
If you want to simulate it,use


DECLARE @MaxID int
SELECT @MaxID=MAX(CustomerNumber)
FROM Customers
SELECT COALESCE(@MaxID,0) + 1 AS NextIDValue

please be aware that for batch insertions you've to generate bunch of new id values and above method wont work.
For that you need to make use of a temporary table with identity column itself.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 01/29/2013 :  21:24:30  Show Profile  Reply with Quote
It's highly likely you really want to use an "Identity" column for this, or, in SQL 2012, an actual SEQUENCE object which is even better. If you intend to use the number and write it back then be aware that Visakah's method only really works reliably for single user. With multiple users (connections actually) you either end up gummed up serialised behind locks or reusing numbers depending on your isolation model.
Go to Top of Page

abhijitheie
Starting Member

2 Posts

Posted - 01/30/2013 :  00:23:17  Show Profile  Reply with Quote
LoztInSpace,

Can u explain how to do it step by step?
Go to Top of Page

DonAtWork
Flowing Fount of Yak Knowledge

2161 Posts

Posted - 01/30/2013 :  07:06:16  Show Profile  Reply with Quote
http://blogs.msdn.com/b/askjay/archive/2012/10/08/sequence-objects-in-sql-2012-and-sql-2008.aspx








How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000