如果我想把我查询到的满足条件的记录加上1,2,3,4这样的编号,SQL语句该怎样写呢?

解决方案 »

  1.   

    你是不是要做一個自動編號的功能
    這是我做的一個你可以看看
    ======================================自動編號===================================
    procedure TForm1.BitBtn1Click(Sender: TObject);
    var
       s,m:string;
       i:integer;
    begin
       s:='PH'+ FormatdateTime('yyyymmdd',Now());
       with ADOquery1 do
       begin
           close;
           sql.clear;
           sql.Add('select max(PH) as PHmax from Tbl1');
           open;
       end;
            m:=Trim(ADOquery1.FieldByName('PHmax').value);
            //if isnull(m,0)=true then
            if m=null then
                    s:= s + '001'
            else
            begin
                    i:=strToint(Trim(copy(m,11,3)));                if i<10 then
                            s:= s + '00' + inttostr(i+1)
                    else if i<100 then
                            s:=s + '0' + inttostr(i+1)
                    else
                            s:=s + inttostr(i+1);
            end;
            Label1.caption:=s;
    end;
      

  2.   

    方法一:有主键ID
      select (select count(1)+1 from table where id<a.id) as xuhao ,* from table a order by id
    方法二:
      select identity(int,1,1) as xuhao,* into #t from table 
      select * from #t order by xuhao
    方法三:
      用计算字段
      

  3.   

    谢谢楼上2位。
    但是我用 shengliqiang168(ValorSlq) 的方法一和二,用delphi操作oracle数据库,总是提示缺少表达式,而且方法一的id是指哪个字段呢?法二的#t是什么表?谢谢
      

  4.   

    方法2是可以的;
    在SQL2000中是可以的,然后,语句中的table是指你要操作的表 
      

  5.   

    oracle数据库就更简单了
    select rownum xuhao,t.* from 你的表 t
      

  6.   

    谢谢楼上的回答,我是用delphi7+oracle9i开发一个小程序,我用了select rownum,col1,col2
    from tables; 这个sql来实现我说的功能,现在问题来了,这条sql在oracle的sql plus中能按照预期的实现,但是用在delphi中的adoquery的sql语句中执行后在dbgrid里只显示一个只有rownum字段而没有col1和col2的空表,虽然有与查询结果记录数相同的行数,但每行的rownum值都为空,这是为什么呢?谢谢!