1,A01 先進來  然後找出 B01 C01  ,B02 C02 
2,B01 C01 找出 A01,A02    A02是新的
3,A02  找找出 B01 C01 ,B03 C03  B03 C03是新的 
3,B03 C03 找出A02,A04,A05 A04,A05 是新的這麼循環檢索 直到沒有新的出來為止

解决方案 »

  1.   

    create table tbkey(key1 varchar(10),key2 varchar(10),key3 varchar(10))
    insert into tbkey select 'A01','B01','C01' 
    union all select  'A01','B02','C02' 
    union all select  'A02','B01','C01' 
    union all select  'A02','B03','C03' 
    union all select  'A04','B03','C03' 
    union all select  'A04','B02','C02' 
    union all select  'A05','B03','C03' 
    union all select  'A07','B05','C05' 
    select * into abc from tbkey where 1=2  --创建一个表结构一样的表用来存放所要的结果insert into abc select * from tbkey where key1='A01'
    while @@rowcount>0
    begin
       insert into abc 
       select * from tbkey where key1 in(
       select key1 from tbkey 
          where key1 not in(select key1 from abc) 
          and ( key2 in (select key2 from abc) or key3 in(select key3 from abc) ))
    end
    select * from abc/*
    key1       key2       key3       
    ---------- ---------- ---------- 
    A01        B01        C01
    A01        B02        C02
    A02        B01        C01
    A02        B03        C03
    A04        B03        C03
    A04        B02        C02
    A05        B03        C03(所影响的行数为 7 行)
    */
      

  2.   


    CREATE TABLE [dbo].[test01](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [k1] [varchar](50) NOT NULL,
    [k2] [varchar](50) NOT NULL,
    [k3] [varchar](50) NOT NULL
    ) ON [PRIMARY]goinsert into test01 
    select 'A01','B01','C01' 
    union all select  'A01','B02','C02' 
    union all select  'A02','B01','C01' 
    union all select  'A02','B03','C03' 
    union all select  'A04','B03','C03' 
    union all select  'A04','B02','C02' 
    union all select  'A05','B03','C03' 
    union all select  'A07','B05','C05' 
    gocreate PROCEDURE [dbo].[proc_test01] (@k varchar(50))
    AS
    begin declare @k1 varchar(50);
    declare @k2 varchar(50);
    declare @k3 varchar(50); set @k1 = @k ; --表#test01_k1用来存放符合条件的k1
    create table #test01_k1(k1 varchar(50) not null ,is_new int not null);
    insert into #test01_k1 values(@k,1);
    --表#test01_k2k3临时存放对应的k1检索出来的k2,k3
    create table #test01_k2k3(k2 varchar(50) not null,k3 varchar(50) not null); while (select count(*) from #test01_k1 where is_new=1) > 0 -- 一直到没有新的
    begin
    -- 通过新的k1, 检索出对应的k2,k3,存到临时表#test01_k2k3
    insert into #test01_k2k3 select k2,k3 from test01 
    where k1 in (select k1 from #test01_k1 where is_new=1);
    -- 将临时表中的标识为新的k1设置为已经处理过的
    update #test01_k1 set is_new = 0 where is_new=1;
    -- 通过前一步检索出来的k2,k3来查询对应它们的是否有新的k1,有就存放到#test01_k1中,同时标识为新
    insert into #test01_k1 
    select o.k1,1 from test01 o inner join #test01_k2k3 t 
    on o.k2=t.k2 and o.k3=t.k3 
    where o.k1 not in (select k1 from #test01_k1);
    -- 清空临时表#test01_k2k3中的数据,因为它们已经没有意义了
    delete #test01_k2k3 ;
    end select o.* from test01 o where o.k1 in (select k1 from #test01_k1); drop table #test01_k1;
    drop table #test01_k2k3;
    endgoexec proc_test01 'A01'