SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO CREATETRIGGER[testTrigger]ON[dbo].[Phone] FORINSERT, UPDATE AS declare@Level2char(10), @Level3char(10), @phonechar(11), @firstchar(1), @secondchar(1), @thirdchar(1), @lastchar(1) select@phone=phonenum from inserted select@first=substring(@phone,8,1) select@second=substring(@phone,9,1) select@third=substring(@phone,10,1) select@last=substring(@phone,11,1) -- 普号无4 if (charindex('4', @phone )=0) begin set@Level2='普号' set@Level3='无4' end --差号含4 if (charindex('4', @phone)!=0) begin set@Level2='差号' set@Level3='号码含4' end --差号尾数含4 if (charindex('4', substring(@phone,8,5) )!=0) begin set@Level2='差号' set@Level3='尾数含4' end --134 if (substring(@phone,0,4) ="134") begin set@Level2='差号' set@Level3='134号段' end --ABAB if (@first=@thirdand@second=@lastand@first<>@second ) begin set@Level2='特号' set@Level3='ABAB' end --AABB if ( @first=@secondand@third=@lastand@first<>@third ) begin set@Level2='特号' set@Level3='AABB' end --ABC if ( convert(int,@last)=convert(int,@third)+1andconvert(int,@third)=convert(int,@second)+1andcharindex('4',@phone)=0) begin set@Level2='优选号码' set@Level3='ABC' end --ABCD if ( convert(int,@last)=convert(int,@third)+1andconvert(int,@third)=convert(int,@second)+1andconvert(int,@second)=convert(int,@first)+1 ) begin set@Level2='特号' set@Level3='ABCD' end --AAAB if (@first=@secondand@second=@thirdand@third<>@lastandcharindex('4',@phone)=0) begin set@Level2='优选号码' set@Level3='AAAB' end --ABBA if (@first=@lastand@second=@thirdand@first<>@secondandcharindex('4',@phone)=0) begin set@Level2='优选号码' set@Level3='ABBA' end --AA if (@third=@lastandcharindex('4',@phone)=0) begin set@Level2='优选号码' set@Level3='AA' end --AAA if (@second=@thirdand@third=@last) begin set@Level2='特号' set@Level3='AAA' end --AAAA if (@first=@secondand@second=@thirdand@third=@last) begin set@Level2='特号' set@Level3='AAAA' end update phone set phone.hlr=PhoneNumLevel1.hlr,level1=ModelNum,level2=@level2,level3=@level3 from PhoneNumLevel1 INNERJOIN phone on right(NumRange,7)=left(phonenum,7) where phonenum=@phone GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO