先请大家看看我的方法:填加一个临时表:TempTable
结构如下:ID_B   AllFee   Company   Currency
          NULL    300      Comp1      RMB  程序中的具体做法如下:
1)执行Sql语句,合并记录并插入到TempTable中。
   Insert Into TempTable select SUM(Fee) as AllFee, Company, Currency from 表A
   Group By Company, Currency.2)通过循环语句,为TempTable中的记录插入不同的 ID_B3)通过循环语句,逐条读取TempTable中的记录,并且:
   a) 插入记录到表B中
   b) 更新表A中相应记录的ID_B(Company和Currency相同的记录).4)清空TempTable.这样虽然可以实现我需要的功能。但是却出现两个毛病:
1、执行该功能的时候,速度很慢。2、出现一个很奇怪的异常:
   测试的记录大概有500条,其中有7条记录在第2步出现异常,“需要更新或者删除的记录不存在”小弟现在急需解决该问题,请高手帮忙!

解决方案 »

  1.   

    表A中的费用累加插入表B应该没有问题,ID_B就用自动计数好了,插入完毕得到ID_B,对表A Update一下就可以了:
    Update 表A Set ID_B='得到的ID_B' Where Company='comp1' AND Currency='RMB'
    当然要循环完成。
      

  2.   

    为什么没人帮我啊……HELP……
      

  3.   

    insert into B(company,currency,allfee,id_b)
    select  company,currency,fee=sum(fee),id_b='t'+min(id_a) from  A  group by company,currency
    --------------在表a中插入id_b-------------------------------------------
    alter  table  a  add id_b  varchar(10) null    ---------在表a中加id_b字段
    update a  set  id_b=b.id_b  from a  inner join b  on  a.company=b.company  and a.currency  b.currency   ---把值插入進去
      

  4.   

    1.更新表A中的ID_B
      update A set ID_B = Company + Currency
    2.在表B中插入记录
      insert into B
      select ID_B, sum(Fee), Currency, Company
      from A
      Group by ID_B, Currency, Company
      

  5.   

    --表B的ID_B用自动编号字段
    alter table B drop column id_b
    alter table B add id_b int identity(1, 1)--存储过程
    create proc p_test
    as
    delete from binsert into b( AllFee, Currency, Company )
    select sum(Fee) as AllFee, Company, Currency 
    from a
    Group By Company, Currencyupdate a  
    set id_b = b.id_b 
    from a inner join b on a.company = b.company  and a.Currency = b.Currency
    GO--测试
    select * from a
    select * from b