求助,怎么在where条件中用case 语句?
demo:
declare @city varchar(20)
select id,city from tb where id=@id
后面加case语句 if(@city='全部') 后面不加何条件,
结果是【select id,city from tb where id=@id 】
else city = @city;
结果是【select id,city from tb where id=@id and city = @city】
注明: 实际情况select 语句很长很长。想通过一条语句解决该问题。不分开写。
------解决方案--------------------
DECLARE @city VARCHAR(20)
DECLARE @id INT
SELECT id ,
city
FROM tb
WHERE id = @id
AND city = CASE WHEN @city = '全部' THEN city
ELSE @city
END