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')
select 's40',artcode,syscreated
from tb a where not exists
(select 1 from tb where artcode=a.artcode and magcode='s40')
@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 #