create table t3(id3 int identity(1,1),id1 int,id2 int,name2 varchar(10))insert into t3(id1,id2,name2)
select id1,id2,name2
from t1,t2
where t1.g1=t2.g1

解决方案 »

  1.   


    select id3=identity(int,1,1), a.id1,b.id2,b.name2 into #temp  from t1 a,t2 b
    insert t3 select * from #temp
    drop table #temp
      

  2.   

    如果再加个一个条件:
    此时若T1改变如下:
    T1
    id1    name1    G1
    1      w1      1
    2      w2      1删除
    3      w3      2
    4      w4      2
    5      w5      1 如何使T3也做相应的改变呢?T3
    id3 id1 id2 name2
    1    1   1   aa
    2    5   1   aa
    3    1   2   bb
    4    5   2   bb
    5    3   3   cc
    6    4   3   cc 
      

  3.   

    或者说,在T3中正常记录存在的情况下,我如何防止再一次执行sql语句时,在T3中产生重复的记录?
      

  4.   

    在插入前是否可以用Left/Right Join去判断是否已经插入了记录,如果记录已经存在,则不插入;若不存在则插入。这个sql语句怎么写呢?
      

  5.   

    insert into t3(id1,id2,name2)
    select id1,id2,name
    from t1,t2
    where t1.g1=t2.g1
    1 1 aa
    5 1 aa
    1 2 bb
    5 2 bb
    3 3 cc
    4 3 cc