这是该函数应返回 1 的示例测试数据,其他任何应返回 0 的数据:
Inventor: Raj Patel
Attorney: Raj Patel
Inventor: Patel; Raj
Attorney: Patel
Inventor: Patel; R
Attorney: Patel; Raj
Inventor: Patel; Raj, Madnani; Raj
Attorney: Patel; Raj
Inventor: Patel; Raj
Attorney: Patel; R
**Eg.** Select dbo.Match('Patel; R','Patel; Raj')
所有这些执行都应该返回 1:
Select dbo.Match('Raj Patel','Raj Patel')
Select dbo.Match('Patel; Raj','Patel')
Select dbo.Match('Patel; R',' Patel; Raj')
Select dbo.Match('Patel; Raj, Madnani; Raj','Patel; Raj')
Select dbo.Match('Patel; Raj','Patel; R')
应该返回 1
这是我使用太多游标的匹配功能:
ALTER FUNCTION [dbo].[Match]
(
@Subj1 varchar(8000),
@Subj2 varchar(8000)
)
RETURNS bit
AS
BEGIN
Set @Subj1 = IsNull(@Subj1,'')
Set @Subj2 = IsNull(@Subj2,'')
If @Subj1 = '' Or @Subj2 = ''
Begin
Return 0
End
If Lower(@Subj1) = Lower(@Subj2)
Begin
Return 1
End
Declare Subj1NamesCurr Cursor For --all separate names
Select * From dbo.Split(@Subj1,',')
Declare Subj2NamesCurr Cursor SCROLL For --all separate names
Select * From dbo.Split(@Subj2,',')
Open Subj1NamesCurr
Open Subj2NamesCurr
Declare @Sub1Names varchar(8000)
Declare @Sub2Names varchar(8000)
Declare @Sub1NamePart varchar(8000)
Declare @Sub2NamePart varchar(8000)
Declare @Sub1PartCount tinyint = 0
Declare @Sub2PartCount tinyint = 0
Declare @Sub1NamesPart TABLE(Data varchar(8000))
Declare @Sub2NamesPart TABLE(Data varchar(8000))
Declare @MatchCount int = 0
Declare @TempCount int = 0
Fetch From Subj1NamesCurr INTO @Sub1Names --fetch 1st name from 1st subject
Insert into @Sub1NamesPart
Select * From dbo.Split(@Sub1Names,';') --get names part from 1st subject's row
Select @Sub1PartCount = Count(*) From @Sub1NamesPart
While @@Fetch_Status = 0 --each names of 1st subject
Begin
Fetch First From Subj2NamesCurr into @Sub2Names
While @@Fetch_Status = 0 --each names of 1st subject
Begin
Declare Sub1NameCurr Cursor For
Select * From @Sub1NamesPart --name parts of 1st subject
OPEN Sub1NameCurr
Fetch From Sub1NameCurr into @Sub1NamePart
Insert into @Sub2NamesPart
Select * From dbo.Split(@Sub2Names,';')
Select @Sub2PartCount = Count(*) From @Sub2NamesPart
Set @MatchCount = 0
While @@Fetch_Status = 0 --splitted name of 1st subject
Begin
Declare Sub2NameCurr Cursor For
Select * From @Sub2NamesPart --name parts of 2nd subject
OPEN Sub2NameCurr
Fetch From Sub2NameCurr into @Sub2NamePart
Set @TempCount = 0
While @@Fetch_Status = 0 --splitted name of 2nd subject
Begin
Set @TempCount = @TempCount + 1
If dbo.Trim(Lower(@Sub1NamePart)) = dbo.Trim(Lower(@Sub2NamePart))
Begin
Set @MatchCount = @MatchCount + 1
If @Sub2PartCount = 1
Begin
Return 1
End
End
Else If Lower(Left(dbo.Trim(@Sub1NamePart),1)) = Lower(dbo.Trim(@Sub2NamePart)) Or
Lower(Left(dbo.Trim(@Sub2NamePart),1)) = Lower(dbo.Trim(@Sub1NamePart))
Begin
Set @MatchCount = @MatchCount + 1
End
Fetch Next From Sub2NameCurr into @Sub2NamePart
Delete from @Sub2NamesPart
Insert into @Sub2NamesPart
Select * From dbo.Split(@Sub2Names,';')
End
If @MatchCount = @Sub2PartCount
Begin
Return 1
End
CLOSE Sub2NameCurr
DEALLOCATE Sub2NameCurr
Fetch Next From Sub1NameCurr into @Sub1NamePart
Delete from @Sub1NamesPart
Insert into @Sub1NamesPart
Select * From dbo.Split(@Sub1Names,';') --get names part from 1st subject's row
Select @Sub1PartCount = Count(*) From @Sub1NamesPart
End
CLOSE Sub1NameCurr
DEALLOCATE Sub1NameCurr
End
End
Close Subj1NamesCurr
Deallocate Subj1NamesCurr
Close Subj2NamesCurr
Deallocate Subj2NamesCurr
Return 0
END
编辑:为了避免混淆,Trim 只是一个对字符串执行 LTrim 和 RTrim 的函数。就是这样。