Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Sequential Numbering Expand / Collapse
Author
Message
Posted Friday, November 28, 2003 12:00 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, June 6, 2014 2:06 PM
Points: 1,040, Visits: 277
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/glarsen/sequentialnumbering.asp

Gregory A. Larsen, MVP

Need SQL Server Examples check out my website at http://www.sqlserverexamples.com
Post #18652
Posted Saturday, December 6, 2003 3:25 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 5:59 PM
Points: 31,082, Visits: 15,529
Some good techniques if you really need to do this in SQL, but my argument often for this process is to have the client do it. VB, Crystal, ASP, etc. all process the rows sequentially and it is very easy and much less resource intensive to have the client calculate the sequential numbers.

Steve Jones
sjones@sqlservercentral.com
http://www.sqlservercentral.com/columnists/sjones
The Best of SQL Server Central.com 2002 - http://www.sqlservercentral.com/bestof/
www.dkranch.net







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #89082
Posted Monday, December 8, 2003 8:01 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, June 6, 2014 2:06 PM
Points: 1,040, Visits: 277
I whole heartly agree with the client side solution when ever possible.

Gregory A. Larsen, DBA
Contributor to 'The Best of SQLServerCentral.com 2002' book. Get a copy here: http:www.sqlservercentral.com/bestof/purchase.asp

Need SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples


Gregory A. Larsen, MVP

Need SQL Server Examples check out my website at http://www.sqlserverexamples.com
Post #89083
Posted Monday, December 8, 2003 3:46 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, October 18, 2012 11:17 AM
Points: 49, Visits: 97
Another way to do this is using Indentity, not using it as a property of a column, but as function in a TSQL statement. Using your example:


select Identity(int,1,1) as rank, Hiredate, LastName, Firstname
into #hireDate
from northwind.dbo.employees
where Title = 'Sales Representative'
order by HireDate

Select cast(rank as char(4)) as Rank,
cast(hiredate as varchar(23)) as HireDate,
LastName,
FirstName from #HireDate order by 1

Drop table #HireDate


I think this could be a good function to have in mind, specially when you want a quickly solution.

Roberto Figueroa
figaroATgcmexDOTcom




Post #89084
Posted Monday, May 3, 2004 5:44 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:14 PM
Points: 35,266, Visits: 31,756

Just a note... The IDENTITY function can only be used when the INTO clause (as Figaro did) is also present or you will get the following error...

Server: Msg 177, Level 15, State 1, Line 1
The IDENTITY function can only be used when the SELECT statement has an INTO clause.



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #114058
Posted Tuesday, December 7, 2004 2:35 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Sunday, November 10, 2013 11:52 AM
Points: 877, Visits: 188

There is another technique. Gert Drapers did a straw poll of the audience at one of his PASS presentations this year to see who knew about this technique and very few did...hence its probably worth passing on here.

Stick the following into QA and run:

--I have a table:
USE tempdb
GO

CREATE TABLE MyTable (
   MyTableID int NULL,
   [Name] varchar(35) NOT NULL
)

--I populate some data
INSERT MyTable ([Name]) VALUES ('Jamie')
INSERT MyTable ([Name]) VALUES ('David')
INSERT MyTable ([Name]) VALUES ('Harold')
GO

--I then run the following to populate MyTableID
DECLARE @vKeyCounter int
SET @vKeyCounter = (SELECT ISNULL(MAX(MyTableID), 0) FROM MyTable)
UPDATE MyTable
SET @vKeyCounter = MyTableID= @vKeyCounter + 1
WHERE MyTableID IS NULL
OPTION (MAXDOP 1)

--View the results
SELECT *
FROM MyTable

The clever bit is the SET part of the UPDATE statement which also changes the variable @vKeyCounter.

Have a go with this and let me know what you think. I'd appreciate feedback.

 



Jamie Thomson
http://sqlblog.com/blogs/jamie_thomson
Post #149750
Posted Tuesday, December 7, 2004 2:39 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Sunday, November 10, 2013 11:52 AM
Points: 877, Visits: 188

I've just realised that this thread is over a year old. I hope someone reads it!!!

There was a link to it in the 2004-12-06 newsletter which is why I happen to be replying to it now!

 

 



Jamie Thomson
http://sqlblog.com/blogs/jamie_thomson
Post #149752
Posted Tuesday, November 28, 2006 7:51 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 18, 2014 9:29 AM
Points: 13, Visits: 3

Jamie --

   I like the approach because of its simplicity; however, if the records are numbered in the order that they appear in the table. This may be good approach if coupled with a temporary table built on some select statement (in a SP for example) so that the records are ordered before the sequential number is applied for some display purpose.

Art

DOH! I just noticed that your comment was two years old.




Post #326017
Posted Tuesday, November 28, 2006 9:30 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:14 PM
Points: 35,266, Visits: 31,756
Heh... better late than never...

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #326105
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse