Multiple Select Statements using SQL Server 2005 “WITH” Statement

前端 未结 5 1493
清歌不尽
清歌不尽 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条回答
  •  萌比男神i
    2020-12-03 18:08

    I believe that Common Table Expressions are only valid for immediate use which is why you are getting an error for the "SELECT Count(*) FROM MyBigProducts". In order to reuse a CTE you should use a temporary table instead

    DECALRE @BigProducts TABLE (...[declaration omitted]...)
    
    INSERT INTO @BigProducts
    SELECT * 
    FROM Products 
    WHERE Size='Big'
    
    
    SELECT Name FROM @BigProducts
    SELECT Count(*) FROM @BigProducts
    

    Please correct me if I am wrong.

提交回复
热议问题