【问题标题】:Insert a duplicate record in SQL Server with identity column ON在标识列 ON 的 SQL Server 中插入重复记录
【发布时间】:2023-03-18 05:19:01
【问题描述】:

这是我的要求:我想在我的 SQL Server 表中插入一些重复的记录。名为 QID 的列是 PK - 标识列自动递增 1。

我尝试了以下方法:

insert into qtable 
    select top 1 * 
    from qtable

导致错误:

只有在使用列列表并且 IDENTITY_INSERT 为 ON 时,才能为表 'qtable' 中的标识列指定显式值

然后我尝试不使用标识列,包括表中的所有列。

INSERT INTO qtable  
    SELECT TOP 1   
        col1, col2, col3, col4...  
    FROM
        qtable 

这给了我插入的值,但 col1 值被转移到 col2 和 col2 到 col3 等等,并且 col1 具有标识值。

如何插入重复记录但标识列必须自动递增?

【问题讨论】:

  • 你不能。它是主键。这意味着每一行都可以由该列中的值唯一标识。

标签: sql-server


【解决方案1】:

您必须为插入语句指定列。

像这样:

INSERT INTO qtable (col1,col2,col3) 
select top 1 col1
   , col2
   , col3 
from qtable

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    相关资源
    最近更新 更多