我正在尝试在where子句中执行select,其中case语句的结果是列名。
这是我尝试过的:
declare @lang int -- this is passed to the stored procedure
declare @productname nvarchar(300) -- this is passed to the stored procedure
select productid from products where
case
when @lang = 1 then producten
when @lang = 2 then productit
when @lang = 3 then productde
end product like @productname
所以,对于英语(@lang = 1),我想:
select productid from products where producten like @productname
虽然我遇到了错误但不确定我做错了什么。
答案 0 :(得分:1)
你想这样说:
case when @lang = 1 then producten
when @lang = 2 then productit
when @lang = 3 then productde
end like @productname
或者,或许更清楚:
when @lang = 1 and producten like @productname or
@lang = 2 and productit like @productname or
@lang = 3 and productde like @productname
答案 1 :(得分:0)
您可以将查询重写为:
declare @lang int -- this is passed to the stored procedure
declare @productname nvarchar(300) -- this is passed to the stored procedure
select productid from products
where 1= case when @lang = 1 and @productname like producten then 1
when @lang = 2 and @productname like productit then 1
when @lang = 3 and @productname like productde then 1
end