views:

28

answers:

1

I have this query:

select  count (convert(varchar(50), TmpDate, 103 )),convert(varchar(50), TmpDate, 103 ) 
from MEN
group by  TmpDate
order by TmpDate desc

and I need to count how many rows it returns

how I can combine select count (..... and query1 ? I need it in one query

thanks in advance

+1  A: 

You can use SELECT @@ROWCOUNT to return the number of rows affected by the previous SQL statement.

See http://www.brettb.com/SQL_Help_Rowcount_Rows_Affected.asp

UPDATE: The simplest way to return a rowcount for a query is just to use a subquery:

SELECT COUNT(*) FROM (
    -- My sql statement
) AS ResultSet

For example:

SELECT COUNT(*) FROM (
    select  count (convert(varchar(50), TmpDate, 103 )),convert(varchar(50), TmpDate, 103 ) 
    from MEN
    group by  TmpDate
    order by TmpDate desc
) AS ResultSet

There are probably clever ways of figuring out the rowcount by looking at your query, however using a subquery like this doesn't require you to think too much about what the query that your executing is.

Kragen
thank's for the help !, but if i dont want to see the result of the query - only the number of the rows ?
Gold
@Kragen, it should be @@ROWCOUNT..
Ramesh Vel
thank's again !! but i get this error: Msg 1033, Level 15, State 1, Line 14The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
Gold