我有MS SQL存储过程,它旨在以XML显示结果,显示所有'sales'
的申请人。
页面上有过滤器可以深入挖掘搜索结果(sString
)。
理想的行为是:
sString
为null
,则应返回所有结果; sString
为not null
,则应显示sString
以显示找到的字段。 SP无法按预期工作:无论sString
值如何,它始终返回所有结果。
SP的MS SQL代码:
WITH OrderedMatches AS
(
SELECT ROW_NUMBER() OVER (ORDER BY MB.Member_Registered DESC) AS RowNumber,
MB.Member_ID,
MB.Member_Name, MB.Member_Mobile, MB.Member_Propertytosell, MB.Member_Propertytosell_Details, MB.Member_ExistingBuytoLet, MB.Member_TalkingPoints,
MB.Member_Registered, MB.Member_Funding, MB.Member_Active, MB.Member_Last_Contacted, MB.Member_Situation
FROM dbo.Member_Base MB INNER JOIN dbo.Member_Criteria MC ON MC.Member_ID = MB.Member_ID
WHERE MB.Member_Active = 1 AND MC.Criteria_Section = 'sales'
AND (
@sType = 'a'
OR (
@sType = 'b' AND MB.Member_Propertytosell = 1
)
OR (
@sType = 'c' AND MB.Member_ExistingBuytoLet = 1
)
)
OR (
MB.Member_Name LIKE '%' + @sString + '%' OR MB.Member_Mobile LIKE '%' + @sString + '%' OR MB.Member_Propertytosell_Details LIKE '%' + @sString + '%'
)
)
SELECT
(
SELECT
OM.Member_ID as "@id",
OM.Member_Name as "@appname",
OM.Member_Mobile as "@contact",
OM.Member_Propertytosell as "@propts",
OM.Member_Propertytosell_Details as "@proptsdetails",
OM.Member_ExistingBuytoLet as "@existingBTL",
OM.Member_TalkingPoints as "@talkingpoints",
OM.Member_Registered as "@registered",
OM.Member_Funding as "@funding",
OM.Member_Active as "@active",
OM.Member_Last_Contacted as "@lastcontact",
OM.Member_Situation as "@situation"
FROM OrderedMatches OM
WHERE OM.RowNumber Between @nstart AND @nend
FOR XML path ('applicant'), TYPE
),
(
SELECT COUNT(*) as "@titems"
FROM OrderedMatches
FOR XML path ('meta')
)
FOR XML PATH ('')
END
我认为SP错了,但无法确切地看到哪一部分。
有人有任何建议吗?
答案 0 :(得分:0)
它返回了所有内容,因为OR
条件与sString
中的其他条件之间有WHERE
。将您的WHERE
子句更改为:
WHERE MB.Member_Active = 1 AND MC.Criteria_Section = 'sales'
AND (
@sType = 'a'
OR (
@sType = 'b' AND MB.Member_Propertytosell = 1
)
OR (
@sType = 'c' AND MB.Member_ExistingBuytoLet = 1
)
)
AND (@sString IS NULL
OR
(
MB.Member_Name LIKE '%' + @sString + '%' OR MB.Member_Mobile LIKE '%' + @sString + '%' OR MB.Member_Propertytosell_Details LIKE '%' + @sString + '%'
)
)