如何使用通配符参数和其他参数来过滤数据?
How to filter data using wildcard parameter along with other parameters?
ALTER PROCEDURE MyProcName
@IsMatch varchar(50)
AS
SELECT
IMS_PolicyNumber, Rater_PolicyNumber
FROM
tblPolicies
WHERE
(@IsMatch = 'No Match' AND Rater_PolicyNumber <> IMS_PolicyNumber OR Rater_PolicyNumber IS NULL) -- returns No Match records
OR (@IsMatch = 'Match' AND Rater_PolicyNumber = IMS_PolicyNumber) --Returns Matching Records
OR (@IsMatch = 'Both') --Returns both: matching and no matching records
但现在我想添加通配符参数 @Rater_PolicyNumber
这样用户就可以只输入 Rater_PolicyNumber
的首字母并过滤结果,或者如果用户将其留空,则忽略它参数:
AND @Rater_PolicyNumber LIKE COALESCE('%'+ @Rater_PolicyNumber+'%','')
但我很难让它与 @IsMatch
参数一起工作:
如果我在 @Rater_PolicyNumber
中传递值,则以下内容有效,但如果我将其留空 - 它 return 包含所有记录,而不仅仅是不匹配的记录。
WHERE
((@IsMatch = 'No Match' AND Rater_PolicyNumber <> IMS_PolicyNumber OR Rater_PolicyNumber is null)
OR Rater_PolicyNumber LIKE COALESCE('%'+ @Rater_PolicyNumber+'%',''))
我如何才能 return 'No Match'、'Match' 和 'Both' 记录并仅在用户输入时按 @Rater_PolicyNumber
值过滤它们。
谢谢
使用多个 and
和 or
可能会很棘手。我已经应用了一些格式并在这些部分周围添加了括号 return 记录,因为它们最初被 returned,但是如果 @Rater_PolicyNumber
不为空,它也会应用 like
.
select IMS_PolicyNumber
, Rater_PolicyNumber
from tblPolicies
where (
Rater_PolicyNumber like '%'+ @Rater_PolicyNumber+'%'
or Rater_PolicyNumber is null
or @Rater_PolicyNumber is null
)
and (
(@IsMatch = 'No Match'
and (Rater_PolicyNumber<>IMS_PolicyNumber
or Rater_PolicyNumber is null)
)
-- returns No Match records
or (@IsMatch= 'Match' and Rater_PolicyNumber=IMS_PolicyNumber)
-- Returns Matching Records
or (@IsMatch = 'Both')
--Returns both: matching and no matching records
)
你能试试这个吗?
SELECT
IMS_PolicyNumber, Rater_PolicyNumber
FROM
tblPolicies
WHERE CHARINDEX(@IsMatch,CASE WHEN Rater_PolicyNumber = IMS_PolicyNumber THEN 'Match,Both' ELSE 'No MATCH,Both' END)>0
ALTER PROCEDURE MyProcName
@IsMatch varchar(50)
AS
SELECT
IMS_PolicyNumber, Rater_PolicyNumber
FROM
tblPolicies
WHERE
(@IsMatch = 'No Match' AND Rater_PolicyNumber <> IMS_PolicyNumber OR Rater_PolicyNumber IS NULL) -- returns No Match records
OR (@IsMatch = 'Match' AND Rater_PolicyNumber = IMS_PolicyNumber) --Returns Matching Records
OR (@IsMatch = 'Both') --Returns both: matching and no matching records
但现在我想添加通配符参数 @Rater_PolicyNumber
这样用户就可以只输入 Rater_PolicyNumber
的首字母并过滤结果,或者如果用户将其留空,则忽略它参数:
AND @Rater_PolicyNumber LIKE COALESCE('%'+ @Rater_PolicyNumber+'%','')
但我很难让它与 @IsMatch
参数一起工作:
如果我在 @Rater_PolicyNumber
中传递值,则以下内容有效,但如果我将其留空 - 它 return 包含所有记录,而不仅仅是不匹配的记录。
WHERE
((@IsMatch = 'No Match' AND Rater_PolicyNumber <> IMS_PolicyNumber OR Rater_PolicyNumber is null)
OR Rater_PolicyNumber LIKE COALESCE('%'+ @Rater_PolicyNumber+'%',''))
我如何才能 return 'No Match'、'Match' 和 'Both' 记录并仅在用户输入时按 @Rater_PolicyNumber
值过滤它们。
谢谢
使用多个 and
和 or
可能会很棘手。我已经应用了一些格式并在这些部分周围添加了括号 return 记录,因为它们最初被 returned,但是如果 @Rater_PolicyNumber
不为空,它也会应用 like
.
select IMS_PolicyNumber
, Rater_PolicyNumber
from tblPolicies
where (
Rater_PolicyNumber like '%'+ @Rater_PolicyNumber+'%'
or Rater_PolicyNumber is null
or @Rater_PolicyNumber is null
)
and (
(@IsMatch = 'No Match'
and (Rater_PolicyNumber<>IMS_PolicyNumber
or Rater_PolicyNumber is null)
)
-- returns No Match records
or (@IsMatch= 'Match' and Rater_PolicyNumber=IMS_PolicyNumber)
-- Returns Matching Records
or (@IsMatch = 'Both')
--Returns both: matching and no matching records
)
你能试试这个吗?
SELECT
IMS_PolicyNumber, Rater_PolicyNumber
FROM
tblPolicies
WHERE CHARINDEX(@IsMatch,CASE WHEN Rater_PolicyNumber = IMS_PolicyNumber THEN 'Match,Both' ELSE 'No MATCH,Both' END)>0