where子句不能与WITH一起使用

时间:2013-06-13 07:00:10

标签: sql sql-server sql-server-2008

当我写其中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'

2 个答案:

答案 0 :(得分:5)

with与此无关。您只是在最后的Prse子句中引入SELECT - 并且您无法从WHERE子句引用此类列(因为WHERE之前逻辑运行 / em> 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'

答案 1 :(得分: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'