﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Gregory Larsen / Article Discussions / Article Discussions by Author  / Sequential Numbering / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Mon, 20 May 2013 07:46:31 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Sequential Numbering</title><link>http://www.sqlservercentral.com/Forums/Topic18652-90-1.aspx</link><description>Heh... better late than never... &lt;img src='images/emotions/biggrin.gif' height='20' width='20' border='0' title='Big Grin' align='absmiddle'&gt;</description><pubDate>Tue, 28 Nov 2006 09:30:00 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Sequential Numbering</title><link>http://www.sqlservercentral.com/Forums/Topic18652-90-1.aspx</link><description>&lt;P&gt;Jamie --&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: Verdana"&gt;   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.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Art&lt;/P&gt;&lt;P&gt;&lt;img src='images/emotions/doze.gif' height='20' width='20' border='0' title='Doze' align='absmiddle'&gt; DOH! I just noticed that your comment was two years old.&lt;/P&gt;</description><pubDate>Tue, 28 Nov 2006 07:51:00 GMT</pubDate><dc:creator>A Narro</dc:creator></item><item><title>RE: Sequential Numbering</title><link>http://www.sqlservercentral.com/Forums/Topic18652-90-1.aspx</link><description>&lt;P&gt;I've just realised that this thread is over a year old. I hope someone reads it!!!&lt;/P&gt;&lt;P&gt;There was a link to it in the 2004-12-06 newsletter which is why I happen to be replying to it now!&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Tue, 07 Dec 2004 02:39:00 GMT</pubDate><dc:creator>Jamie Thomson</dc:creator></item><item><title>RE: Sequential Numbering</title><link>http://www.sqlservercentral.com/Forums/Topic18652-90-1.aspx</link><description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Stick the following into QA and run:&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#33bb33&gt;&lt;FONT color=#119999&gt;--I have a table:&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#3333dd&gt;USE&lt;/FONT&gt; tempdbGO&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#3333dd&gt;CREATE TABLE&lt;/FONT&gt; MyTable (   MyTableID &lt;FONT color=#3333dd&gt;int&lt;/FONT&gt; &lt;FONT color=#999999&gt;NULL&lt;/FONT&gt;,   [Name] &lt;FONT color=#3333dd&gt;varchar&lt;/FONT&gt;(35) &lt;FONT color=#999999&gt;NOT NULL&lt;/FONT&gt;)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#119999&gt;--I populate some data&lt;/FONT&gt;&lt;FONT color=#3333dd&gt;INSERT&lt;/FONT&gt; MyTable ([Name]) &lt;FONT color=#3333dd&gt;VALUES&lt;/FONT&gt; ('Jamie')&lt;FONT color=#3333dd&gt;INSERT&lt;/FONT&gt; MyTable ([Name]) &lt;FONT color=#3333dd&gt;VALUES&lt;/FONT&gt; ('David')&lt;FONT color=#3333dd&gt;INSERT&lt;/FONT&gt; MyTable ([Name]) &lt;FONT color=#3333dd&gt;VALUES&lt;/FONT&gt; ('Harold')GO&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#119999&gt;--I then run the following to populate MyTableID&lt;/FONT&gt;&lt;FONT color=#3333dd&gt;DECLARE&lt;/FONT&gt; @vKeyCounter &lt;FONT color=#3333dd&gt;int&lt;/FONT&gt;&lt;FONT color=#3333dd&gt;SET&lt;/FONT&gt; @vKeyCounter = (&lt;FONT color=#3333dd&gt;SELECT&lt;/FONT&gt; &lt;FONT color=#dd33dd&gt;ISNULL&lt;/FONT&gt;(MAX(MyTableID), 0) &lt;FONT color=#3333dd&gt;FROM&lt;/FONT&gt; MyTable)&lt;FONT color=#3333dd&gt;UPDATE&lt;/FONT&gt; MyTable&lt;FONT color=#3333dd&gt;SET&lt;/FONT&gt; @vKeyCounter = MyTableID= @vKeyCounter + 1&lt;FONT color=#3333dd&gt;WHERE&lt;/FONT&gt; MyTableID &lt;FONT color=#3333dd&gt;IS&lt;/FONT&gt; &lt;FONT color=#999999&gt;NULL&lt;/FONT&gt;&lt;FONT color=#3333dd&gt;OPTION&lt;/FONT&gt; (MAXDOP 1)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#119999&gt;--View the results&lt;/FONT&gt;&lt;FONT color=#3333dd&gt;SELECT&lt;/FONT&gt; *&lt;FONT color=#3333dd&gt;FROM&lt;/FONT&gt; MyTable&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;The clever bit is the SET part of the UPDATE statement which also changes the variable @vKeyCounter.&lt;/P&gt;&lt;P&gt;Have a go with this and let me know what you think. I'd appreciate feedback.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Tue, 07 Dec 2004 02:35:00 GMT</pubDate><dc:creator>Jamie Thomson</dc:creator></item><item><title>RE: Sequential Numbering</title><link>http://www.sqlservercentral.com/Forums/Topic18652-90-1.aspx</link><description>&lt;P&gt;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...&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#ff1111&gt;Server: Msg 177, Level 15, State 1, Line 1&lt;/FONT&gt;The IDENTITY function can only be used when the SELECT statement has an INTO clause.&lt;/FONT&gt;&lt;/P&gt;</description><pubDate>Mon, 03 May 2004 05:44:00 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Sequential Numbering</title><link>http://www.sqlservercentral.com/Forums/Topic18652-90-1.aspx</link><description>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:&lt;font face='Courier New'&gt;select Identity(int,1,1) as rank, Hiredate, LastName, Firstnameinto #hireDatefrom northwind.dbo.employeeswhere Title = 'Sales Representative'order by HireDateSelect cast(rank as char(4)) as Rank, cast(hiredate as varchar(23)) as HireDate,LastName, FirstName from #HireDate order by 1Drop table #HireDate&lt;/font id='Courier New'&gt;I think this could be a good function to have in mind, specially when you want a quickly solution.Roberto FigueroafigaroATgcmexDOTcom </description><pubDate>Mon, 08 Dec 2003 15:46:00 GMT</pubDate><dc:creator>figaro</dc:creator></item><item><title>RE: Sequential Numbering</title><link>http://www.sqlservercentral.com/Forums/Topic18652-90-1.aspx</link><description>I whole heartly agree with the client side solution when ever possible.Gregory A. Larsen, DBAContributor to 'The Best of SQLServerCentral.com 2002' book.  Get a copy here: http:www.sqlservercentral.com/bestof/purchase.aspNeed SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples</description><pubDate>Mon, 08 Dec 2003 08:01:00 GMT</pubDate><dc:creator>Greg Larsen</dc:creator></item><item><title>RE: Sequential Numbering</title><link>http://www.sqlservercentral.com/Forums/Topic18652-90-1.aspx</link><description>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 Jonessjones@sqlservercentral.comhttp://www.sqlservercentral.com/columnists/sjonesThe Best of SQL Server Central.com 2002 - http://www.sqlservercentral.com/bestof/www.dkranch.net</description><pubDate>Sat, 06 Dec 2003 15:25:00 GMT</pubDate><dc:creator>Steve Jones - SSC Editor</dc:creator></item><item><title>Sequential Numbering</title><link>http://www.sqlservercentral.com/Forums/Topic18652-90-1.aspx</link><description>Comments posted to this topic are about the content posted at &lt;A HREF=http://www.sqlservercentral.com/columnists/glarsen/sequentialnumbering.asp&gt;http://www.sqlservercentral.com/columnists/glarsen/sequentialnumbering.asp&lt;/A&gt;</description><pubDate>Fri, 28 Nov 2003 00:00:00 GMT</pubDate><dc:creator>Greg Larsen</dc:creator></item></channel></rss>