各位大俠:    本人現在正在編一個存儲過程,要求把一個表裡a的數據 插入到另一個表b,
同時a的某些列(組合主鍵) 不能已經存在 b中。
    因為 b中部分列a中沒有,所以插入時查詢a的結果必須加入部分列的自定義值!    在這裡我真的搞不清怎麼做。
    我的sql如下出錯!
   
    insert into b(b.c1,b.c2,b.c3,b.c4,b.c5)
    select distinct a.c1,a.c2,a.c3,CONVERT( CHAR(23),getdate(),21),'通過'
    from a
    where (a.c1,a.c2,a.c3) not exist(select c1,c2,c3 from b)
    求各位大俠幫幫忙啊!

解决方案 »

  1.   

    insert into b(b.c1,b.c2,b.c3,b.c4,b.c5)
        select distinct a.c1,a.c2,a.c3,CONVERT( CHAR(23),getdate(),21),'通過'
        from a
        where not exists(select c1 from b Where c1 = a.c1 and c2 = a.c2 and c3 = a.c3)
      

  2.   

    你的意思是,将a表中的全部列插入到一个新建立的b表中,但是这个b表比a表中的列多,同时要b中不能存在a中的数据,判断依据是组合键不能重复
    是这个意思吗?
      

  3.   

    create table a
    (
      c1 varchar(10),
      c2 varchar(20),
      c3 varchar(10),
    )
    insert into a values('1','2','3')
    insert into a values('1','2','3')
    insert into a values('1','2','3')
    insert into a values('1','2','3')select a.*,aa='tongguo' into  b from a
    select * from b我不知道这个是不是你想要的