如题:将A表中的不重复的数据提取出来,再将提取出来的数据插入到表B中,若B表中存在重复的则将B表中的数量字段增加1.两表中的识别码为NO.
谢谢了

解决方案 »

  1.   

    ----先更新
    update b set num = num + 1
    from tableB as b
    inner join table A as a on b.NO = a.NO----再插入
    insert into tableB
    select a.* from tableA as a 
    left join tableB as b on a.NO = b.NO 
    where b.NO is null
      

  2.   

    也就是说将A表的数据用select distinct no,...查询出来,然后将查询出来的数据插入到B表中,若B表中有no相同的则在B表中的qty字段加1
    ok?
      

  3.   

    ----先更新
    update b set num = num + 1
    from tableB as b
    inner join (
    select no from tableA group by no ) as a on b.NO = a.NO----再插入
    insert into tableB(no,num)
    select no,1 from (
    select no from tableA group by no ) as a 
    left join tableB as b on a.NO = b.NO 
    where b.NO is null
      

  4.   

    num-->qty----先更新
    update b set qty = b.qty + 1
    from tableB as b
    inner join (
    select no from tableA group by no ) as a on b.NO = a.NO----再插入
    insert into tableB(no,qty)
    select a.no,1 from (
    select no from tableA group by no ) as a 
    left join tableB as b on a.NO = b.NO 
    where b.NO is null
      

  5.   

    希望能看到舉例來避免理解錯了意思,你下面的解釋還是和上面一樣的。還是猜一下你的需求吧。try
    --先update,再insert--Update
    Update B Set qty = qty + 1 From A Inner Join B On A.no = B.no--Insert
    Insert B Select distinct no,... From A
      

  6.   

    hellowork(一两清风),Haiwer(海阔天空) 你們都加了判斷,去掉重復的no但是樓主的需求又沒有說說明,重復的不用插入。
    看看樓主的選擇吧。
      

  7.   

    --先插
    insert into tableB
    select distinct a.* from tableA as a 
    left join tableB as b on a.NO = b.NO 
    where b.NO is null
    -- 再update
    update tableB A set A.Qty = (select sum(B.Qty) from tableB B where B.no = A.no) 
    --再讀
    select distinct * from tableB
      

  8.   

    好像现在sql版块的反应速度快了。
    以前问题放几天都没人理。
    我国有希望了。