表test
uid   name
A     张三
B     李四
C     王五
D     马六
我要的结果:
id     uid   name
1      A     张三
2      B     李四
3      C     王五
4      D     马六
也就是在前面加一个自动编号

解决方案 »

  1.   

    select identity(int),* from biao
      

  2.   

    --不好意思,上面写错了
    select identity(int)[id],* into # from biao
    select * from #
      

  3.   

    仅当 SELECT 语句中有 INTO 子句时,才能使用 IDENTITY 函数。
      

  4.   

    select id=identity(int,1,1),* into #test from testselect * from #test
      

  5.   

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

  6.   

    select id=(select count(*) from test where uid<=t.uid),* from test t
      

  7.   

    楼上运用的不规范,identity函数还可以从任意一个数值指定增量变化,不一定就是1,虽然默认为1。例如,可以这样写:identity(int,100,1)或者identity(int,2,2)都可以。
      

  8.   

    alter table test add id int identity(1,1)
    --------------------
    还是这个最直接,不知道楼主能否接受改动模型?还是仅仅要一个查询结果?
      

  9.   

    flyskylf(天高云淡) ( ) 信誉:100    Blog  2006-09-03 19:46:00  得分: 0 
       我只是要一个结果  
     ----------------------
    xyxfly(小虾米) ( ) 信誉:100    Blog  2006-09-03 16:53:00  得分: 0   
       select id=(select count(*) from test where uid<=t.uid),* from test t
      
     这个就可以了吧
      

  10.   

    1.使用临时表 
    可以使用select into 创建临时表,在第一列,加入Identify(int,1,1)作为行号,这样在产生的临时表中,结果集就有了行号.也是目前效率最高的方法。这种方法不能用于视图 
    代码: 
    set nocount on
    select IDentify(int,1,1) 'RowOrder',au_lname,au_fname into #tmp
    from authors
    select * frm #tmp
    drop table #tmp 
    2.使用自连接 
    不用临时表,在SQL语句中,动态的进行排序.这种方法用到的连接是自连接,连接关系一般是大于。 
    代码: 
    select rank=count(*), a1.au_lname, a1.au_fname
    from authors a1 inner
    join authors a2 on a1.au_lname + a1.au_fname >= a2.au_lname + a2.au_fname
    group by a1.au_lname, a1.au_fname
    order by count(*) 
    运行结果: rank au_lname au_fname 
    1 Bennet Abraham 
    2 Blotchet-Halls Reginald 
    3 Carson Cheryl 
    4 DeFrance Michel 
    5 del Castillo  Innes 
    6 Dull Ann 
    7 Greene Morningstar 
    ...  ...  ...  缺点: 
    1.使用自联接,所以该方法不适用于处理大量行。它适用于处理几百行。对于大型表,一定要使用索引以避免进行大范围的搜索,或用第一种方法. 
    2.不能正常处理重复值。当比较重复值时,会出现不连续的行编号。如果不希望出现这种现象,可以在电子表格中插入结果时隐藏排序列,而是使用电子表格编号,或用第一种方法