我有一个错误:
无效的列名称Temp
我想在Temp
子句中使用列where
。我该怎么做?
ALTER PROCEDURE [dbo].[R_rpt1]
@ltt int
AS
BEGIN
SELECT ROW_NUMBER() OVER (ORDER BY YeuCauId) STT, a.name,
CASE WHEN a.tt = 0 THEN 0
WHEN (a.tt = 1 AND a.code IN (Select code from SCHEMAB.dbo.databaseB)) THEN 3
WHEN (a.tt = 1 ) THEN 1
WHEN a.tt=2 THEN 2
END as Temp
FROM ViewPhieuYeuCau a
WHERE Temp = @ltt
END
答案 0 :(得分:2)
使用子查询:
ALTER PROCEDURE [dbo].[R_rpt1]
@ltt int
AS
BEGIN
SELECT *
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY YeuCauId) STT, a.name,
CASE
WHEN a.tt = 0 THEN 0
WHEN a.tt = 1 AND a.code IN (Select code from SCHEMAB.dbo.databaseB) THEN 3
WHEN a.tt = 1 THEN 1
WHEN a.tt=2 THEN 2
END as Temp
FROM ViewPhieuYeuCau a
) t
WHERE Temp = @ltt
END
由于WHERE
子句首先在SELECT
之前执行,Temp
子句尚未识别WHERE
。要解决此问题,您必须使用子查询。