table1 str_C
G_Code G_Name G_Amount //字段名
 a1     b1       23
 a2     b2       32
 .       .        .
 .       .        .Table2 STR_C
GCode   GName    GAmount //字段名
  a1     b1       2
  a1     b1       3
  a2     b2       4
   .      .       .
   .      .       .现在是判断Table2 中的GCode字段中的每一行, 如果与Table1中的G_Code字段的值相等,那么把Table2表中GCode字段对应的GAmount字段的值加到Table1表中G_Code字段对应的G_Amount中。(注:Table1表中的记录较多,我试了几种方法但速度不是很理想,但有些方法不能在DelPhi中成功运行)
  

解决方案 »

  1.   

    用Sql,未经测试。select GCode, sum(Table1.GAmount)+sum(Table2.GAmount) as GAmout from Table1,Table2
    where Table1.GCode=Table2.GCode
    group by GCode
      

  2.   

    update Table1 set Table1.G_Amount=Table2.GAmount from Table1,Table2 where
    Table1.G_code=Table2.Gcode;
      

  3.   

    Declare table_cursor CURSOR For 
    select Table2.GAmount from table1,table2 where table1.G_code=table2.Gcode;
    open table_cursor;
    while @@FETCH_STATUS = 0
    begin
    fetch next from table_cursor into @GAmount
    update table where table1.G_Amount=@GAmount
    end
    CLOSE table_cursor
    DEALLOCATE table_cursor
      

  4.   

    update table1 set table1.g_Amount=table1.g_Amount+table2.gAmount
    from table1,table2
    where table1.g_code=table2.gCode
      

  5.   

    楼上的在SQL中能够成功运行,在Delphi中也可以运行,但不能更新G_Amount,不知是什么原因?