我有一表,结构入下:
                     UID(非自增)   NAME
现在我想写一条 SELECT 语句,查询结果如下
                     ID(自动生成的自增列)          UID        NAME请问能不能够实现呢?

解决方案 »

  1.   

    select  ID=identity(int,1,1),* into # from tb
      

  2.   

    临时表select id=identity(int,1,1),uid,name into #t from t
    select * from #t
      

  3.   

    alter table 表名 add  列名 int identity( 1,1)
      

  4.   

    select id=Identity(int,1,1), * into 临时表 from 表
      

  5.   

    还有,上面的语句在存储过程中,用 EXEC(select id=Identity(int,1,1), * into 临时表 from 表
    )执行后,怎么不能用了? 难道作用域只在 EXEC 内?
      

  6.   

    From 《Sql server 联机丛书》:IDENTITY 函数“只用在带有 INTO table 子句的 SELECT 语句中,以将标识列插入到新表中。”
      

  7.   

    噢!是这样,忘了,不好意思,楼上说得对
    select id=Identity(int,1,1), * into 新表 from 表
      

  8.   

    我现在的  select id=Identity(int,1,1), * into 新表 from 表  
    语句是在 存储过程里 拼出来,然后放在 EXEC() 里执行。是不是 临时表的作用域就只在 EXEC() 内了呢?
    有啥办法解决嘛?
      

  9.   

    select id=Identity(int,1,1), * into 新表 from 表  
    这条语句中生成了一个新表,不是临时表。