1

当我写where Prse ='h'时,where 子句不适用于 WITH, 它向我显示错误“列无效”

;with cT(FLDID  ,FLD10  ,FLD610)
as
(
select  * from Table556 
inner join Table555 on table555.FLD9=FLD318
where FLD610=150 
)

select case when fld609 <=12 then 'h' else 's' end as Prse,* from cT 
where Prse ='h'
4

2 回答 2

5

with它无关。您只是Prse在最后一个SELECT子句中引入 - 并且您不能从WHERE子句中引用此类列(因为WHERE逻辑上运行before SELECT)。

你想要:

;with cT(Prse, FLDID  ,FLD10  ,FLD610)
as
(
select case when fld609 <=12 then 'h' else 's' end as Prse, * from Table556 
inner join Table555 on table555.FLD9=FLD318
where FLD610=150 
)

select * from cT 
where Prse ='h'
于 2013-06-13T07:02:55.543 回答
2

试试这个——

;WITH cte AS
(
    SELECT 
          FLDID
        , FLD10
        , FLD610
        , Prse = 
            CASE WHEN FLD609 <= 12 
                THEN 'h' 
                ELSE 's' 
            END  
    FROM dbo.Table556 t 
    JOIN dbo.Table555 t2 ON t2.FLD9 = t.FLD318
    WHERE FLD610 = 150 
)
SELECT *
FROM cte 
WHERE Prse = 'h'
于 2013-06-13T07:10:48.690 回答