Select 根据参数是否为NULL

Select based on whether a parameter is NULL or not

如果客户 ID 不为空,则使用提供的客户 ID 进行过滤,否则显示所有客户。我不知道该怎么做。我正在考虑的一种方法是使用 CASE 语句,但不确定如何编写它。

select * from customer
where case when @customerid is null then show all customers
else customer_id = @customerid

谢谢

做你想做的一种方法是:

select * 
from customer 
where 
    (@customerid is null OR customer_id = @customerid)