|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Saturday, January 19, 2013 8:28 AM
Points: 1,038,
Visits: 255
|
|
|
|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Yesterday @ 2:54 PM
Points: 31,410,
Visits: 13,726
|
|
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
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Saturday, January 19, 2013 8:28 AM
Points: 1,038,
Visits: 255
|
|
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
|
|
|
|
|
SSC 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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 5:33 PM
Points: 32,902,
Visits: 26,783
|
|
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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 2:32 AM
Points: 877,
Visits: 183
|
|
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
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 2:32 AM
Points: 877,
Visits: 183
|
|
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
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, June 15, 2007 9:10 PM
Points: 13,
Visits: 1
|
|
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.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 5:33 PM
Points: 32,902,
Visits: 26,783
|
|
|
|
|