Sql server T-SQL中的SQL Server CASE语句
嗨,我正在处理涉及用例的查询。我不断地出错,不知道为什么。下面是我试过的Sql server T-SQL中的SQL Server CASE语句,sql-server,case,Sql Server,Case,嗨,我正在处理涉及用例的查询。我不断地出错,不知道为什么。下面是我试过的 SELECT CASE when LineItem like '%Apple%' THEN ProductType = 'Apple' when LineItem like '%Orange' THEN ProductType = 'Orange' when LineItem like '%Strawberry' THEN ProductType = 'Red' whe
SELECT
CASE
when LineItem like '%Apple%' THEN ProductType = 'Apple'
when LineItem like '%Orange' THEN ProductType = 'Orange'
when LineItem like '%Strawberry' THEN ProductType = 'Red'
when ProductType like 'Yellow' THEN ProductType
else ProductType = 'White'
end as ProductType
FROM Fruits
我得到的错误在=符号上。
在T-SQL中,WHEN关键字后面不可能有两个不同的列吗?只是语法错误。不需要那么ProductType=“…” 像这样试试
SELECT
ProductType = CASE
WHEN f.LineItem like '%Apple%' THEN 'Apple'
WHEN f.LineItem like '%Orange' THEN 'Orange'
WHEN f.LineItem like '%Strawberry' THEN 'Red'
WHEN f.ProductType like 'Yellow' THEN f.ProductType
ELSE 'White'
END
FROM
dbo.Fruits f;
去掉ProductType=。只需使用其他“白色”,记住像“黄色”这样的ProductType充当=
SELECT
ProductType = CASE
WHEN f.LineItem like '%Apple%' THEN 'Apple'
WHEN f.LineItem like '%Orange' THEN 'Orange'
WHEN f.LineItem like '%Strawberry' THEN 'Red'
WHEN f.ProductType like 'Yellow' THEN f.ProductType
ELSE 'White'
END
FROM
dbo.Fruits f;