我有这样的查询
有一个主要的CASE表达式,如果结果是1,那么我需要执行其他case语句,否则,下面的select语句。此查询无效。任何人都可以帮助我
DECLARE @sportCodeID INT;
SELECT @sportCodeID = sport_code_id FROM jobs dlj where dlj.id = id;
(CASE WHEN (SELECT sport_code_id FROM jobs dlj WHERE dlj.id = id) = 1 THEN
CASE WHEN dl.action = 'C' THEN 'Cricker'
WHEN dl.action = 'F' THEN 'FOOTBALL'
)
(ELSE
(SELECT sport_codes from action_codes ac WHERE ac.id = @sportCodeID )
)END [Action]
答案 0 :(得分:1)
以下是一些可能实现目标的代码
declare @jobs table(id int identity (1,1), sport_code_Id int)
insert @jobs (sport_code_Id)
Select 1 UNION ALL
Select 2 UNION ALL
Select 3 UNION ALL
Select 4
declare @action_codes table (id int identity(1,1), sport_codes char(1))
Insert @action_codes (sport_codes)
Select '1' UNION ALL
Select 'F' UNION ALL
Select 'C' UNION ALL
Select '4'
Select * from @action_codes
Select * from @jobs
DECLARE @sportCodeID INT;
SELECT @sportCodeID = sport_code_id FROM @jobs dlj where dlj.id = 1;
select @sportCodeID
SELECT
CASE sport_codes
WHEN 'F' THEN 'Football'
WHEN 'C' THEN 'Criket'
ELSE sport_codes
END
FROM @action_codes
WHERE id = @sportCodeID
如果您仍有疑问,请使用AdventureWorks数据库查看案例陈述中的示例。
USE AdventureWorks2008;
GO
SELECT ProductNumber, Category =
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
END,
Name
FROM Production.Product
ORDER BY ProductNumber;
GO