有人能告诉我为什么会收到错误: 消息207,级别16,状态1,过程ExtractPDP4FromPDP,第21行 列名称'ContainsEX'无效。
执行以下存储过程时。
CREATE PROCEDURE ExtractPDP4FromPDP
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT PDP.*, LEFT(PDPCode,7) AS PDP7, PDP.Obsolete,
PDP.InvestorPDP, PDP.OnlineReport, PDP.ClientSpecific,
ContainsEX = CASE
WHEN(CHARINDEX(Left(PDPCode,5),'EX')>0) THEN 'True'
ELSE 'False'
END, PDP4 =
CASE
WHEN ContainsEX = 'True' THEN 'E' & SUBSTRING(pdpcode,5,3)
ELSE SUBSTRING(pdpcode,6,3)
END
FROM PDP
WHERE (((PDP.Obsolete)='False') AND ((PDP.InvestorPDP)='True') AND
((PDP.OnlineReport)='False') AND ((PDP.ClientSpecific)='False'));
END
GO
提前谢谢
答案 0 :(得分:7)
Sql Server不允许您引用同一级别的字段。您必须创建一个子查询,例如:
select *,
PDP4 = CASE
WHEN ContainsEX = 'True' THEN 'E' & SUBSTRING(pdpcode,5,3)
ELSE SUBSTRING(pdpcode,6,3)
END
from (
select *, LEFT(PDPCode,7) AS PDP7, PDP.Obsolete,
PDP.InvestorPDP, PDP.OnlineReport, PDP.ClientSpecific,
ContainsEX = CASE
WHEN(CHARINDEX(Left(PDPCode,5),'EX')>0) THEN 'True'
ELSE 'False'
END
from PDP
WHERE (((PDP.Obsolete)='False') AND ((PDP.InvestorPDP)='True') AND
((PDP.OnlineReport)='False') AND ((PDP.ClientSpecific)='False'))
) sub
答案 1 :(得分:1)
定义后,不能立即在语句中使用列。
另一种方法是使用堆叠的CTE来构建表达式或使用嵌套查询:
WITH cte1 AS (
SELECT x, x AS y
FROM t
),cte2 AS (
SELECT x, y, x + y AS z
FROM cte1
)
SELECT x, y, z
FROM cte2
答案 2 :(得分:0)
您的个案陈述不正确。案例当'价值'='价值'然后 '做一些像CONTAINEX ='祖母'END