순서가 중요하지 않은 경우 변수에서 조건을 생성 한 다음 절 like
에서 필터 와 함께 사용할 수 where
있습니다.
declare @Rules table (RID int, RuleValue varchar(50))
insert into @Rules
values
(1,'DDDD+FFFF')
,(2,'DDDD+EEEE+FFFF')
,(3,'BBBB+CCCC')
,(4,'BBBB+DDDD')
,(5,'CCCC+EEEE')
,(6,'BBBB+DDDD')
DECLARE @RuleType VARCHAR(MAX)= 'DDDD+FFFF' ;
select *
from @Rules
where RuleValue like '%' + replace(@RuleType, '+','%') + '%'
결과 :
OP의 코멘트 후 편집
순서가 중요하다면 해결책이 조금 더 까다 롭습니다.
declare @Rules table (RID int, RuleValue varchar(50))
insert into @Rules
values
(1,'DDDD+FFFF')
,(2,'DDDD+EEEE+FFFF')
,(3,'BBBB+CCCC')
,(4,'BBBB+DDDD')
,(5,'CCCC+EEEE')
,(6,'BBBB+DDDD')
DECLARE @RuleType VARCHAR(MAX)= 'DDDD+FFFF+EEEE' ;
--define a table variable to hold every component of the rule type
declare @splittedRules table (SplittedRule nvarchar(max))
--fill the table variable with each component of the rule type
--since you use SQL Server 2012 you must use xml syntax to split the string
insert into @splittedRules
SELECT Split.a.value('.', 'NVARCHAR(MAX)') as SplittedRule
FROM
(
SELECT CAST('<X>'+REPLACE(@RuleType, '+', '</X><X>')+'</X>' AS XML) AS String
) AS A
CROSS APPLY String.nodes('/X') AS Split(a)
--now you can see if each rule matches a single component of the rule type
;with compare as
(
select
r.RID
,r.RuleValue
,spl.SplittedRule
, case when CHARINDEX(spl.SplittedRule, r.RuleValue) > 0 then 1 else 0 end as ok
from
@Rules as r
cross apply
@splittedRules spl
)
--finally you can perform a group by checking
--which rule matches all the components of the rule type
select
RID, RuleValue
from
compare
group by
RID, RuleValue
having
sum (ok)=(select count(*) from @splittedRules)
order by RID
결과 :
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다