2013-06-13 70 views
1

工作where子句中没有与工作时,我写了其中Prse =“H”它显示了我一个错误“无效列”where子句中没有与

;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' 

回答

5

with无关用它。您只在最后的SELECT子句中引入了Prse - 并且您无法参考WHERE子句中的这些列(因为WHERE在逻辑上运行之前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' 
+0

@Dems - 完成。好的结果(我通常不会在我的CTE中使用明确的列列表) –

+0

thankx Damien_The_Unbeliever –

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' 
+3

你曾经*提供任何你已经改变或解释为什么?你似乎只有“尝试这一个”或某个变体和一些代码,然后问问题的人必须仔细研究以确定你已经改变了什么。当然,他们没有什么学习机会,因为他们只会发现你已经改变了什么 - 从来没有*为什么*。 –

+0

thankx Damien_The_Unbeliever –

+0

@Mir Shakeel Hussain,我不是达米安。 :) – Devart