Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 空参数时的情况_Sql_Select_Parameters_Case - Fatal编程技术网

Sql 空参数时的情况

Sql 空参数时的情况,sql,select,parameters,case,Sql,Select,Parameters,Case,上面的查询没有正常工作, as参数有时可能为空 运算符不支持通配符。您应该改用like操作符: declare @customer varchar(50) select cateogry, product_Name from product join order on product.id = order.product_id join customer on order.customer_id = cutstomer.id where category = 'Tea' and cu

上面的查询没有正常工作,
as参数有时可能为空

运算符不支持通配符。您应该改用
like
操作符:

declare @customer varchar(50)

select cateogry, product_Name 
from product 
join order 
on product.id = order.product_id
join customer 
on order.customer_id = cutstomer.id 
where category = 'Tea'
and customer_name =
case 
when @customer is not null 
then @customer 
else 
'%'
end 
或者,您可以使用
操作员将其短路:

SELECT category, product_Name 
FROM   product 
JOIN   order ON product.id = order.product_id
JOIN   customer ON order.customer_id = cutstomer.id 
WHERE  category = 'Tea' AND
       customer_name LIKE -- Here!
       CASE WHEN @customer IS NOT NULL THEN @customer 
       ELSE '%'
       END

=
运算符不支持通配符。您应该改用
like
操作符:

declare @customer varchar(50)

select cateogry, product_Name 
from product 
join order 
on product.id = order.product_id
join customer 
on order.customer_id = cutstomer.id 
where category = 'Tea'
and customer_name =
case 
when @customer is not null 
then @customer 
else 
'%'
end 
或者,您可以使用
操作员将其短路:

SELECT category, product_Name 
FROM   product 
JOIN   order ON product.id = order.product_id
JOIN   customer ON order.customer_id = cutstomer.id 
WHERE  category = 'Tea' AND
       customer_name LIKE -- Here!
       CASE WHEN @customer IS NOT NULL THEN @customer 
       ELSE '%'
       END

当参数不确定是否有值时使用like

SELECT category, product_Name 
FROM   product 
JOIN   order ON product.id = order.product_id
JOIN   customer ON order.customer_id = cutstomer.id 
WHERE  category = 'Tea' AND
       (@customer IS NULL OR customer_name = @customer)

当参数不确定是否有值时使用like

SELECT category, product_Name 
FROM   product 
JOIN   order ON product.id = order.product_id
JOIN   customer ON order.customer_id = cutstomer.id 
WHERE  category = 'Tea' AND
       (@customer IS NULL OR customer_name = @customer)

这当然有帮助。有些行返回比空数据集好。这当然有帮助。有些行返回的数据比空数据集好,这是非标准SQL。您使用的是哪种数据库管理系统?这是非标准SQL。您使用的是哪种数据库管理系统?