Groups | Blog | Home
all groups > sql server (microsoft) > may 2006 >

sql server (microsoft) : rowcount help


D.B.
5/20/2006 5:25:54 AM
i'm trying to get total rows found by query that uses top clause...

for example:
select top 10 myTable.* from myTable where myTable.number > 200

let's say there are 13 rows matching that condition, and by using
@@rowcount my result would be: 10.

is there any way to get total row count, without affecting the TOP
clause??? i believe that the mysql equivalent would be
SQL_CALC_FOUND_ROWS().

tnx...
Jens
5/21/2006 3:00:41 AM
No if you use the TOP clause, the affected rows will be 10. There is
not background information on the query.

HTH, Jens Suessmeyer.

---
http://www.sqlserver2005.de
---
Mike C#
5/21/2006 1:25:37 PM
One way to do it is run the query without the TOP clause, but use COUNT(*)
to get the total # of rows:

DECLARE @sql_calc_found_rows INT
SELECT @sql_calc_found_rows = COUNT(*) FROM myTable WHERE myTable.number >
200

That will give you the number of rows matching your condition.

[quoted text, click to view]

SQL
5/22/2006 6:39:10 AM
You can do this
select top 10 *,(select count(*) from myTable where number > 200 ) as
Totalcount
from myTable m where m.number > 200

example in pubs
select top 10 *, (select count(*) from authors) as AuthorCount from
authors


Denis the SQL Menace
http://sqlservercode.blogspot.com/
AddThis Social Bookmark Button