How to include the total number of returned rows in the resultset from SELECT T-SQL command?

前端 未结 7 1560
一整个雨季
一整个雨季 2020-12-09 09:29

I would like to ask if there is a way to include the total number of rows, as an additional column, in the returned result sets from a TSQL query using also the Row_Nu

7条回答
  •  忘掉有多难
    2020-12-09 10:00

    SELECT  n ,
            COUNT(*) OVER ( PARTITION BY 1 )
    FROM    ( SELECT    1 AS n
              UNION ALL
              SELECT    2 AS n
            ) AS t
    

    Note that @@ROWCOUNT gives you row count from the previous command. Run this:

    SELECT    1 AS n;
    
    SELECT  n ,
            @@ROWCOUNT
    FROM    ( SELECT    1 AS n
              UNION ALL
              SELECT    2 AS n
            ) AS t
    

提交回复
热议问题