我有以下脚本:
DECLARE @PlanId NVARCHAR(255) = ''
DECLARE @PlanName NVARCHAR(255) = ''
DECLARE @ActionID NVARCHAR(255) = ''
DECLARE @ActionDescription01 NVARCHAR(255) = ''
DECLARE @ActionDescription02 NVARCHAR(255) = ''
DECLARE @ActionDescription03 NVARCHAR(255) = ''
DECLARE @ActionDescription04 NVARCHAR(255) = ''
DECLARE @ActionDescription05 NVARCHAR(255) = ''
select ap.plan_id ,ap.plan_name ,apa.action_id ,apa.action_description
from dbo.ACTION_PLAN ap
LEFT JOIN ACTION_PLAN_LINK AS apl ON apl.PLAN_ID = ap.PLAN_ID
LEFT JOIN dbo.ACTION_PLAN_ACTION AS apa ON apa.ACTION_ID = apl.ACTION_ID
WHERE
(ap.PLAN_ID = @PlanId or @PlanId = '')
and (ap.PLAN_NAME = @PlanName or @PlanName = '')
and (apa.ACTION_ID = @ActionID or @ActionID = '')
and (
( (len(@ActionDescription01)>1 and apa.ACTION_DESCRIPTION like ('%' + @ActionDescription01+ '%') ) )
or ( (len(@ActionDescription02)>1 and apa.ACTION_DESCRIPTION like ('%' + @ActionDescription02+ '%') ) )
or ( (len(@ActionDescription03)>1 and apa.ACTION_DESCRIPTION like ('%' + @ActionDescription03+ '%') ) )
or ( (len(@ActionDescription04)>1 and apa.ACTION_DESCRIPTION like ('%' + @ActionDescription04+ '%') ) )
or ( (len(@ActionDescription05)>1 and apa.ACTION_DESCRIPTION like ('%' + @ActionDescription05+ '%') ) )
)
上述脚本在使用操作说明进行搜索时工作正常,但不允许我仅搜索plan_id,plan_name或action_id。我希望能够搜索任何一个或组合的参数。我还想在动作描述where子句中添加我使用or运算符的原因,为每个搜索的动作带来更多结果。例如,我搜索plan_id 100,然后在actiondescription01中输入'inform',我得到一个结果,然后在actiondescription02参数中输入'order',我得到3个结果,因为有两个与订单有关,一个与通知相关但它们都是一个plan_id的一部分(如果输入了plan_id)。
我希望这是有道理的,有人可以提供帮助。上面的脚本完全按照我的意愿工作,但由于某种原因,我无法独立搜索PlanId,PlanName或ActionId,但如果我与一个动作描述参数一起搜索,它们就可以工作。
答案 0 :(得分:1)
我认为你必须改变这些行
or ( (len(@ActionDescription02)>1 and apa.ACTION_DESCRIPTION like ('%' + @ActionDescription02+ '%') ) )
这样的事情
or (
len(@ActionDescription02)=0
or ( len(@ActionDescription02)>0
and apa.ACTION_DESCRIPTION like '%' + @ActionDescription02+ '%' )
)
答案 1 :(得分:1)
您的(
)
比您需要的更多
and ( (
正在杀死它。如果没有或该组中的or
为真,那么and
为假。
select ap.plan_id ,ap.plan_name ,apa.action_id, apa.action_description
from dbo.ACTION_PLAN ap
LEFT JOIN ACTION_PLAN_LINK AS apl ON apl.PLAN_ID = ap.PLAN_ID
LEFT JOIN dbo.ACTION_PLAN_ACTION AS apa ON apa.ACTION_ID = apl.ACTION_ID
WHERE (ap.PLAN_ID = @PlanId or @PlanId = '')
and (ap.PLAN_NAME = @PlanName or @PlanName = '')
and (apa.ACTION_ID = @ActionID or @ActionID = '')
and ( ( len(@ActionDescription01) > 1 and apa.ACTION_DESCRIPTION like '%' + @ActionDescription01 + '%' )
or ( len(@ActionDescription02) > 1 and apa.ACTION_DESCRIPTION like '%' + @ActionDescription02 + '%' )
or ( len(@ActionDescription03) > 1 and apa.ACTION_DESCRIPTION like '%' + @ActionDescription03 + '%' )
or ( len(@ActionDescription04) > 1 and apa.ACTION_DESCRIPTION like '%' + @ActionDescription04 + '%' )
or ( len(@ActionDescription05) > 1 and apa.ACTION_DESCRIPTION like '%' + @ActionDescription05 + '%') )
)
修复
and ( ( len(@ActionDescription01) = 0 and len(@ActionDescription02) = 0 ...)
or ( len(@ActionDescription01) > 1 and apa.ACTION_DESCRIPTION like '%' + @ActionDescription01 + '%' )
or ( len(@ActionDescription02) > 1 and apa.ACTION_DESCRIPTION like '%' + @ActionDescription02 + '%' )
or ( len(@ActionDescription03) > 1 and apa.ACTION_DESCRIPTION like '%' + @ActionDescription03 + '%' )
or ( len(@ActionDescription04) > 1 and apa.ACTION_DESCRIPTION like '%' + @ActionDescription04 + '%' )
or ( len(@ActionDescription05) > 1 and apa.ACTION_DESCRIPTION like '%' + @ActionDescription05 + '%') )
)