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 2008 Forums
 Transact-SQL (2008)
 row count number as a column

Author  Topic 

safderalimd
Starting Member

32 Posts

Posted - 2010-07-07 : 13:53:53
How can I get row_count as column along with other columns in result set

For example.

Select col1, col2
from table

I need to add third column to this result set to give number of rows in the same result set. Any easy way to get this with out using group by?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-07 : 13:57:55
You can use the ROW_NUMBER() function to achieve that.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

safderalimd
Starting Member

32 Posts

Posted - 2010-07-07 : 14:46:16
For example if we have 5 rows, row_number will give 1,2,3,4 and 5.

I want value of 5 in each row, which is I want row count value in a seperate column for each row.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-07-07 : 14:53:53
Oh didn't realize that's what you wanted. How about you pass the result set to your application, count the number of rows (can use @@ROWCOUNT too), and then add the count to the result set on the application end?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -