Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Lopaka
Starting Member
48 Posts |
Posted - 2008-08-07 : 16:47:06
|
Is there a way to query a table and have the select do a row count automatically.create table #temp(title nchar(1))insert into #temp(title)select 'a' unionselect 'b' unionselect 'c' unionselect 'd' unionselect ???, titlefrom #temp--output1 a2 b3 c4 dThank you...:)Robert R. Barnes |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-08-07 : 17:10:19
|
in 2005 this is easy with ROWNUMBER()IN 2000You can do a SELECTIDENTITY(int, 1,1) AS RowNumber, [title]FROM#temp Note I don't have a sql server at the moment so this syntax might not be 100%-------------Charlie |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-07 : 23:04:40
|
the SELECT IDENTITY required SELECT INTO . . . syntax to workselect row_no = (select count(*) from #temp x where x.title <= t.title), titlefrom #temp t KH[spoiler]Time is always against us[/spoiler] |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-08-08 : 02:51:40
|
quote: Originally posted by Lopaka Is there a way to query a table and have the select do a row count automatically.create table #temp(title nchar(1))insert into #temp(title)select 'a' unionselect 'b' unionselect 'c' unionselect 'd' unionselect ???, titlefrom #temp--output1 a2 b3 c4 dThank you...:)Robert R. Barnes
Where do you want to show the data?MadhivananFailing to plan is Planning to fail |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-08-08 : 04:38:02
|
quote: the SELECT IDENTITY required SELECT INTO . . . syntax to work
Yes you are absolutely right. That'll teach me for posting code at home!khtan's code is cool but it requires unique values in #temp otherwise you'll get duplicate row numbers. I think it's also quite slow on large data sets.You could try-- Drop the #temp table if it existsIF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #tempCREATE TABLE #temp ( [title] CHAR(1) )INSERT INTO #temp SELECT 'a'UNION SELECT 'b'UNION SELECT 'c'UNION SELECT 'd'-- Create a store table to keep track of row numbersDECLARE @store TABLE ( [RowNumber] INT IDENTITY(1,1) , [value] CHAR(1) )-- Insert into the @store and generate the row numbersINSERT INTO @storeSELECT [title]FROM #tempORDER BY -- INSERT YOUR ORDER BY HERE [title] ASC--Display the resultsSELECT * FROM @store In your stored proc or whatever,This is more complex but it will offer good performance. If you have a truly large set of data, replace the table variable with another temp table instead.Regards,-------------Charlie |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-08 : 05:33:55
|
the best method of doing this is ofcourse Madhivanan's way. Do it in your front end application where you are displaying the data KH[spoiler]Time is always against us[/spoiler] |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2008-08-08 : 06:27:46
|
quote: the best method of doing this is ofcourse Madhivanan's way
It usually is.-------------Charlie |
 |
|
|
|
|
|
|