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。您使用的是哪种数据库管理系统?