想在已存在的表中增加一个ID列,并让它自动的增加生成:

  1. 办法一、在修改表时把Identity Specification下的Identify Increment设置为yes,并设置Identity Seed种子为1。
  2. 办法二、执行SQL语句:
    alter table tablename add id int identity(1,1)

若要在查询中添加自增列,可以:

  1. 添加一列连续自增的ID,可用如下查询语句:
    select row_number() over (order by getdate()) as id, * from tablename
  2. 使用关键字IDENTITY:

    select identity(int,1,1) as nid,* into #t from tablename;
    Select * from #t;

  注意:

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

    

   (2) 如果表中已有自增ID列,将无法使用 SELECT INTO 语句将标识列添加到临时表,原因是该表的列 'ID' 已继承了标识属性。

      

posted on 2021-07-13 14:15  HandsomeFa  阅读(11807)  评论(0编辑  收藏  举报