查询出的数据用dbgrid1显示,想写入另一个表时按dbgrid1某列中相同数据的行数来附加在另一表的数据后
如:
20050121     ????      20050121001
20050121     ????      20050121002
20050121     ????      20050121003
20050121     ????      20050121004
20050121     ????      20050121005
20050121     ????      20050121006
20050122     ????      20050122001
20050122     ????      20050122002
20050122     ????      20050122003
20050122     ????      20050122004
20050123     ????      20050123001
20050123     ????      20050123002
20050123     ????      20050123003
20050123     ????      20050123004
20050123     ????      20050123005
20050123     ????      20050123006
20050123     ????      20050123007
就是说第一列中相同的记录在第三列中的后三位按顺序依次排列。

解决方案 »

  1.   

    你的dbgrid1是以什么来做数据源?select * from 表 where id=:id
      

  2.   

    我是从SQL SEVER中取数据向Access中写的,ACCESS表中有列需要流水号,所以我想用
    adoquery2.fieldbyname('lsh').asstring:=adoquery1.fieldbyname('rq').as string+'00'+i;
    我用了个全局变量i
    var
    i:integer;
    我想将i依次填充,可我还不会用数组,请哪位哥们帮我实现。分全给你。
      

  3.   

    var
      strA,strB:string;
      j:integer;
    begin
      strA:=adoquery1.fieldbyname('rq').asstring;
      strB:='';
      for j := 1 to (3 - strA)) do
      begin
        strB := strA + '0';
      end;
      adoquery2.fieldbyname('lsh').asstring := 
        strB + IntToStr(StrToInt(strA) + i);//i你的全局变量
    end;
      

  4.   

    --在SQL Server数据库中直接处理可以这样写:
    --示例数据
    create table tb(a char(8))
    insert tb select 20050121
    union all select '20050121'
    union all select '20050121'
    union all select '20050121'
    union all select '20050121'
    union all select '20050121'
    union all select '20050122'
    union all select '20050122'
    union all select '20050122'
    union all select '20050122'
    union all select '20050123'
    union all select '20050123'
    union all select '20050123'
    union all select '20050123'
    union all select '20050123'
    union all select '20050123'
    union all select '20050123'
    go--查询处理
    select id=identity(int),a into #t from tb
    select a,b=a+right(1000+(select count(*) from #t where a=a.a and id<=a.id),3)
    from #t a
    drop table #t
    go--删除测试
    drop table tb /*--测试结果a        b              
    -------- -------------- 
    20050121 20050121001
    20050121 20050121002
    20050121 20050121003
    20050121 20050121004
    20050121 20050121005
    20050121 20050121006
    20050122 20050122001
    20050122 20050122002
    20050122 20050122003
    20050122 20050122004
    20050123 20050123001
    20050123 20050123002
    20050123 20050123003
    20050123 20050123004
    20050123 20050123005
    20050123 20050123006
    20050123 20050123007(所影响的行数为 17 行)
    --*/
      

  5.   

    var
      strA,strB:string;
      j:integer;
    begin
      strA:=adoquery1.fieldbyname('rq').asstring;
      strB:='';
      for j := 1 to (3 - strA)) do  //这句有点不懂,3-strA是什么意思?
      begin
        strB := strA + '0';
      end;
      adoquery2.fieldbyname('lsh').asstring := 
        strB + IntToStr(StrToInt(strA) + i);//i你的全局变量
    end;
    我的句子中strB这一块的没问题,但i这一块的问题存在,我现在只能用i:=1;.......I:=i+1;来解决。
    而且在3-strA处出现这个错误提示:
    [错误] Unit1.pas(134): Incompatible types: 'String' and 'Integer'
      

  6.   

    改成3-strtoint(strA)后可以运行,但我的全局变量i就是想取的idid相同的记录的顺序,你这体现不出来。select id=identity(int),a into #t from tb  //建立临时表?
    select a,b=a+right(1000+(select count(*) from #t where a=a.a and id<=a.id),3)
    from #t a
    drop table #t
    #t是个临时表对吗?我想将adoquery1从SQL中查询出来的结果用adoquery2写入到access中,你的意思是不是要再写一条SQL语句来取相同idid的记录总数到临时表?