我做了测试表:
create table tmp (id BIGINT NOT NULL IDENTITY PRIMARY KEY)
现在我想插入一个值:
insert into tmp () values();
它抱怨:
SQL Error [102] [S0001]: Incorrect syntax near ')'.
我无法插入 ID,因为它失败并显示:
insert into tmp (id) values(1);
-- SQL Error [544] [S0001]: Cannot insert explicit value for identity column
-- in table 'tmp' when IDENTITY_INSERT is set to OFF.
DEFAULT
语法 forVALUES
也被禁止:
insert into tmp values(default);
-- SQL Error [8101] [S0001]: An explicit value for the identity column in table 'tmp'
-- can only be specified when a column list is used and IDENTITY_INSERT is ON.
尝试用
要显式指定列名称,在特殊情况下,请尝试使用
SET IDENTITY_INSERT ON
(1. 并且不要忘记将其设置回 off
SET IDENTITY_INSERT tmp OFF
)(2. 您为列提供的值
id
,因为它是主键,应该是唯一的):所有脚本:
数据库小提琴