在商店程序中,我将代码设为
SELECT *
FROM [Order] O
INNER JOIN Users U ON O.UserId=U.UserId
INNER JOIN State S ON S.StateId=O.Billing_StateId
INNER JOIN OrderStatus OS ON O.OrderStatusId=OS.OrderStatusId
WHERE (CASE WHEN ISNULL(@Name,'')='' THEN 0=0 ELSE O.Billing_FirstName LIKE '%' + @Name + '%' OR O.Billing_LastName LIKE'%' + @Name + '%' END)
AND (CASE WHEN ISNULL(@Email,'')='' THEN 0=0 ELSE O.Billing_Email LIKE '%' + @Email + '%' END)
但是由于语法不正确=
而给出错误我不知道在设置那么0 = 0时我做错了什么?
如果错了,我应该在那里写什么,我的查询是否正常?
如果没有@Name参数,那么它将不会检查第一个条件并检查下一个条件
答案 0 :(得分:3)
你可以试试这个。
WHERE (@Name IS NULL OR( O.Billing_FirstName LIKE '%' + @Name + '%' OR O.Billing_LastName LIKE'%' + @Name + '%' ))
AND (@Email IS NULL OR ( O.Billing_Email LIKE '%' + @Email + '%'))
答案 1 :(得分:0)
试试这个:
SELECT *
FROM [Order] O
INNER JOIN Users U ON O.UserId=U.UserId
INNER JOIN State S ON S.StateId=O.Billing_StateId
INNER JOIN OrderStatus OS ON O.OrderStatusId=OS.OrderStatusId
WHERE
(NULLIF(@Name,'') IS NULL OR O.Billing_FirstName LIKE '%' + @Name + '%' OR O.Billing_LastName LIKE'%' + @Name + '%')
AND (NULLIF(@Email,'') IS NULL OR O.Billing_Email LIKE '%' + @Email + '%')
答案 2 :(得分:-1)
您的 CASE 声明错误
在大小写条件下,您必须定义将替换条件的新参数。
看到这个(我不知道它的工作与否)
WHERE
(CASE
WHEN ISNULL(@Name,'')='' THEN 0
WHEN O.Billing_FirstName LIKE '%' + @Name + '%' OR O.Billing_LastName LIKE'%' + @Name + '%' THEN 1
END as cond1) = 0 (*or 1, i dont know what you need)
AND ........