我用DBGrid来显示查询结果。如何实现在每次查询结果Grid显示的第一列建立一个序号字段,从1开始编号,2,3……按照网友qianguob(qianguob) 和: helodd(可爱)的帮助我使用了下面语句去实现:
select identity(int,1,1)  AS XH , COL1...  into #tmp from  table
select #Tmp
但总是提示identity(int,1,1)错误。我现在的SQL语句是:
SELECT 姓名,日期,内容,加分 FROM measure
WHERE .......
ORDER BY 日期如果要在DBGrid的第一列加入序号列,代码该作何改动呢?谢谢!

解决方案 »

  1.   

    select identity(int,1,1)  AS XH,column1,column2 into #tmp from  mytable
    --此句中Select出来的mytable表中的列不能有主键否则就会出现错误,因为Select into语句会自动向新建表中继承原始表的标识属性
    select #Tmp
    --这句应该该为select * from #tmp
      

  2.   

    如果一定要把mytable中的主键字段也Select出来的话,请在该语句前加
    SET IDENTITY_INSERT mytalbe off
      

  3.   


    Select no=(select count(*)+1 from measure b where b.姓名<a.姓名),a.姓名,a.日期,a.内容,a.加分 from measure a
    Order by a.日期注意姓名为关键字段
      

  4.   

    这样做一定能行!
    1.为数据集增加一个计算字段(假定你取名为mySort),字段类型为fkCalculated
    2.在DBGrid的显示顺序中,把mySort放到第一列
    3.在DBGrid的OnDrawColumnCell事件中加入下列代码
    if Column.Index = 0 then
      with DBGrid1.Canvas do
        begin
          FillRect(Rect);
          TextOut(Rect.Left+2, Rect.Top+2, IntToStr(DBGrid1.DataSource.DataSet.RecNo));
        end;