Create table aa (ID varchar(20),ItemCode varchar(20),BOMUsage varchar(20))
GO
insert into aa(ID,ItemCode,BOMUsage)
select 'S20011','13134313', '3' union all
select '215131', 'S20011', '3' union all
select 'Z10211', 'Z10211' , '3' union all
select 'Z10311', 'Z10211', '3' union all
select 'Z10411', 'Z10311' , '3' union all
select 'Z10051', '1313131', '3' union all
select 'Z10811', 'Z10051', '3' union all
select 'Z21011', 'Z15411' , '3' union all
select 'Z13211', '1313131' , '3' union all
select 'Z10431', 'Z11211', '3' union all
select 'Z14511', 'Z14511' , '3' union all
select 'Z15411', '1313131', '3' union all
select 'Z11211', '1313131', '3' union all
select 'S20011', '13134313' , '3' union all
select '215131', 'S2001' , '3' union all
select 'Z10211', '1313131' , '3' union all
select 'Z10311', 'S20011' , '3' union all
select 'Z10411', '1313131', '3' union all
select 'Z10051', '1313131' , '3' union all
select 'Z10811', '1313131', '3' union all
select 'Z21011', 'Z10811' , '3' union all
select 'Z13211', '1313131' , '3' union all
select 'Z14511', 'Z13211', '3' union all
select 'Z15411', 'Z10431', '3' union all
select 'Z11211', '1313131', '3'
GO
Create table bb (VerID varchar(20),BOMUsg varchar(20))
GO
insert into bb(VerID,BOMUsg)
select 'S20011','z' union all
select '215131','3' union all
select 'Z10211','z' union all
select 'Z10311','3' union all
select 'Z10411','3' union all
select 'Z10051','3' union all
select 'Z10811','3' union all
select 'Z21011','z' union all
select 'Z13211','3' union all
select 'Z10431','z' union all
select 'Z14511','3' union all
select 'Z15411','3' union all
select 'Z11211','z'
数据如上,想在想用递归方法用aa表的ItemCode去匹配bb表的VerID ,得出的bb表的BOMUsg如果为z的话就把为z的数据取出来放入随便一张表里而匹配出还是为3的话就继续往下递归匹配bb表出现z的话也是把为z的数据取出来放入刚刚那一张表里,为3的话继续递归,中间如果出现BOMUsg为NULL的数据不管,只要继续往下递归,最后能得出BOMUsg都为NULL的数据,我会对BOMUsg为NULL的数据和为z的数据进行处理。求各位大神帮帮忙!