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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 select autonumber

Author  Topic 

nic
Posting Yak Master

209 Posts

Posted - 2004-10-01 : 16:45:09
Hi,
I need to return a result set that is autonumbered. ie. 1-xx where xx is some number greater than 1. For example, is xx = 6 it would be:
Result
1
2
3
4
5
6

Each number would be a seperate row. I'm not really querying any table, I just need the ordered result set.

Something like this:
select
'hello world' as result

Result
hello world


This is a totally random question but there might be an easy solution....

Nic

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2004-10-01 : 17:54:50
you could creat a temp table that includes a column with an identity seed, insert your data into the temp table and then select from it.

Kind of roundabout way, but it is doable.



-ec
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2004-10-01 : 17:57:04
here is an example of that method

http://www.extremeexperts.com/SQL/Scripts/SQLRowNum.aspx



-ec
Go to Top of Page

Shurgenz
Yak Posting Veteran

51 Posts

Posted - 2004-10-04 : 00:40:58
select t*5+t1 from (select 0 t union select 1 union select 2 union select 3 union select 4) a,
(select 1 t1 union select 2 union select 3 union select 4 union select 5) b

1-25 ... You can use the idea to create own ranges
Go to Top of Page

slacker
Posting Yak Master

115 Posts

Posted - 2004-10-04 : 02:54:13
Too bad you couldnt use that to generate a rownumber against a table.
Go to Top of Page

Shurgenz
Yak Posting Veteran

51 Posts

Posted - 2004-10-04 : 07:07:35
to generate autonumber for table you may use as follows

select t1.filed1, count(t2.field2) from t t1 join t t2 on t1.field2>=t2.field2 and t1.field1=t2.field1
group by t1.field1

if field2 - is a field of datetime or number type, and you wish to count all fields according growing dates or numbers, then this query will autonumber field1 (on wich tables joined in query)

it is just a sample, but, question was in autonumber only...
Go to Top of Page
   

- Advertisement -