select *,产生序号() 序号 from T
T表本身没有序列号的,要求不能对T表进行增加序号字段的操作 希望大家写个函数跟系统函数newid类似,从1开始生成,如查询返回N条记录,就显示到序号到N,如果T表有10行记录,则显示如下
记录字段   序号
记录1       1
记录2       2
记录3       3
...
记录10      10

解决方案 »

  1.   

    这样通用的函数是写不出来的,SQL 2005和ORACLE数据库自带行号函数.
      

  2.   

    SQL 2005自带行号函数。如果表中有唯一标识字段,SQL 2000中可以使用子查询生成这个序号列。
      

  3.   

    可以增加一个标识列作为序列号吧
    ALTER TABLE T ADD ID int IDENTITY(1,1)--查询
    select 记录字段,序号=ID from T
      

  4.   

    select identity(int,1,1) kk,* into #t from T
    select #t
      

  5.   

    如果表中有唯一标识字段,
    则可以select (select count(*) from tableA b where a.id <b.id) as newid from tableA a
      

  6.   

    select identity(int,1,1) kk,* into #t from T
    select * from #t
      

  7.   

    谢谢大家,问题已解决:以pubs数据库authors表为例
    select au_id,(select count(*) 序号 from authors where au_id <=b.au_id) from dbo.authors b
    order by au_id
      

  8.   

    select dd,(select count(*) 序号 from cc where dd<=b.dd) from cc b
    order by dd
      

  9.   

    好像不行,如果 有查询条件的话,结果就不是期望的了啊
    如:
    select dd,(select count(*) 序号 from cc where dd<=b.dd) from cc b
    where dd between * and *
    order by dd
      

  10.   

    SELECT 序号=IDENTITY(int),* Into #M From T
    select * from #M
     前提是T表中没有IDentity定义的列
      

  11.   

    alter table tablename add id int identity(1,1)
      

  12.   

    发表于:2007-05-14 11:10:32 
    God 此贴生命力很强啊!
    2005 以上用排名函数
    2000 用临时表,自动编号列
      

  13.   

    select Row_number() Over(order by a(a为F表中的不重复字段)),* from F
      

  14.   

    create table AA(i int,a varchar(max),dd int)
    insert into AA values(1,'c',1)
    insert into AA values(5,'b',5)
    insert into AA values(8,'d',4)
    insert into AA values(9,'e',5)
    insert into AA values(5,'f',7)select Row_number() Over(order by a),* from AA
      

  15.   

    select xh=Row_number() Over(order by getdate()),* from tb