Multiple Select Statements using SQL Server 2005 “WITH” Statement

前端 未结 5 1490
清歌不尽
清歌不尽 2020-12-03 17:33

I am trying to use the \"WITH\" statement in SQL Server 2005. It seems to work fine if I do something like:

WITH MyBigProducts AS (SELECT * FROM Products WHE         


        
5条回答
  •  攒了一身酷
    2020-12-03 18:08

    As Kane said, the CTE is only available in the SQL statement where it is written. Another possible solution, depending on the specifics of your situation, would be to include the COUNT(*) in the single query:

    ;WITH MyBigProducts AS
    (
         SELECT
              Name,
              COUNT(*) OVER () AS total_count
         FROM
              Products
         WHERE
              Size = 'Big'
    )
    SELECT
         Name,
         total_count
    FROM
         MyBigProducts
    

提交回复
热议问题