insert tb (magcode,artcode,syscreated)
select 's40',artcode,syscreated
from tb a where not exists
(select 1 from tb where artcode=a.artcode and magcode='s40')

解决方案 »

  1.   

    对于所有magcode:select identity(int,1,1) ID,* into # from 表declare @i int
            @artcode varchar(20)
    select  @i=1while @i<=(select max(ID) from #)
    begin
           insert 表
           select B.magcode
                  ,C.artcode
                  ,C.syscreated
           from # A
           join # B on A.artcode=B.artcode
                       and A.ID<>B.ID
           join # C on A.magcode=C.magcode
           where A.ID=@i
                 and not exists(select 1 
                                   from # 
                                      where magcode=B.magcode 
                                            and artcode=C.artcode)       set @i=@i+1
    end 
    drop table #