Sql server 如何使用通配符参数和其他参数筛选数据?
但我很难让它与@IsMatch参数一起工作:Sql server 如何使用通配符参数和其他参数筛选数据?,sql-server,reporting-services,wildcard,Sql Server,Reporting Services,Wildcard,但我很难让它与@IsMatch参数一起工作: ALTER PROCEDURE MyProcName @IsMatch varchar(50) AS SELECT IMS_PolicyNumber, Rater_PolicyNumber FROM tblPolicies WHERE (@IsMatch = 'No Match' AND Rater_PolicyNumber <> IMS_PolicyNu
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中传递值,下面的方法会起作用,但如果我将其留空,它将返回所有记录,而不仅仅是不匹配的记录
AND @Rater_PolicyNumber LIKE COALESCE('%'+ @Rater_PolicyNumber+'%','')
如何返回“不匹配”、“匹配”和“两者”记录,并仅在用户输入时按@Rater_PolicyNumber值对其进行筛选
谢谢使用多个and和or可能会很棘手。我已经应用了一些格式,并在部分周围添加了括号,以返回最初返回的记录,但是如果@Rater_PolicyNumber不为null,它也会应用类似的方法 你能试试这个吗
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
)
谢谢它可以工作,但如果Rater_PolicyNumber=NULL,它不会返回NULL值,我也需要这些值。你认为有可能吗?@Oleg更新为包括或Rater_PolicyNumber在第一组标准中为空非常感谢!工作完美!抱歉耽搁了@奥列格很乐意帮忙!
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