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 2005 Forums
 Transact-SQL (2005)
 attach an auto-increment on-the-fly result column?

Author  Topic 

PaTRiCKDRD
Yak Posting Veteran

55 Posts

Posted - 2007-07-04 : 08:10:52
Hi everyone!

I have a variable that takes an initial value,
e.g. 5

Does anyone know of a way I can add and increment this value to a query result?
i.e. attach a column that takes values 5, 6, 7 and so on to the query result,
sth like this:

SELECT @new_aa + 1, col2, col3 FROM ...
which doesn't work (returns a column having a value of 1 in all rows)!

Thanks in advance!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-04 : 08:44:36
Try the new ROW_NUMBER() function available in SQL Server 2005.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-04 : 08:45:44
SELECT ROW_NUMBER() OVER (ORDER BY Col1) + @New_AA AS <SomeColNameHere>, col2, col3 FROM Table1


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

PaTRiCKDRD
Yak Posting Veteran

55 Posts

Posted - 2007-07-04 : 09:09:07
thank you very much peso!

I feel relieved and grateful!
Go to Top of Page
   

- Advertisement -