想在已存在的表中增加一个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 函数。

    SQL Server 增加自增ID列

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

 

相关文章:

  • 2022-12-23
  • 2021-08-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
  • 2021-04-15
猜你喜欢
  • 2021-07-06
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
  • 2021-09-09
相关资源
相似解决方案