IDENTITY_INSERT SQL 脚本
IDENTITY_INSERT SQL script
情况
我有一个 SQL 脚本,我需要将一些基本数据推送到我的数据库。因此,我使用以下脚本(和其他脚本)。我想为我手动创建的行手动提供主键。
问题
如果我执行脚本,它会提示我需要启用 IDENTITY_INSERT。我添加了这一行 SET IDENTITY_INSERT UserGroups ON;
正如许多示例所使用的那样,但是当我添加它时它仍然会给出相同的错误。
SET IDENTITY_INSERT UserGroups ON;
GO
INSERT INTO UserGroups VALUES (0, 0);
运行 脚本时出错:
An explicit value for the identity column in table 'UserGroups' can only be specified when a column list is used and IDENTITY_INSERT is ON.
问题
我是否还需要更改我的数据库中的某些内容,或者我在脚本中忘记了其他内容以手动添加主键?
详情
我使用 SQL Server 2016 Management Studio。
我为 SQL 个脚本使用 DDL 脚本
我和 Entity Framework 一起工作。
我在这个 table
中有两列
- 主键:Id
- int: GroupHeadId
错误消息告诉您问题所在。
An explicit value for the identity column in table 'UserGroups' can only be specified when a column list is used and IDENTITY_INSERT is ON.
您没有使用列列表。指定列。
如错误所述,您需要一个列列表。
INSERT INTO UserGroups (Id, GroupHeadId)
VALUES (0,0)
情况
我有一个 SQL 脚本,我需要将一些基本数据推送到我的数据库。因此,我使用以下脚本(和其他脚本)。我想为我手动创建的行手动提供主键。
问题
如果我执行脚本,它会提示我需要启用 IDENTITY_INSERT。我添加了这一行 SET IDENTITY_INSERT UserGroups ON;
正如许多示例所使用的那样,但是当我添加它时它仍然会给出相同的错误。
SET IDENTITY_INSERT UserGroups ON;
GO
INSERT INTO UserGroups VALUES (0, 0);
运行 脚本时出错:
An explicit value for the identity column in table 'UserGroups' can only be specified when a column list is used and IDENTITY_INSERT is ON.
问题
我是否还需要更改我的数据库中的某些内容,或者我在脚本中忘记了其他内容以手动添加主键?
详情
我使用 SQL Server 2016 Management Studio。
我为 SQL 个脚本使用 DDL 脚本
我和 Entity Framework 一起工作。
我在这个 table
中有两列- 主键:Id
- int: GroupHeadId
错误消息告诉您问题所在。
An explicit value for the identity column in table 'UserGroups' can only be specified when a column list is used and IDENTITY_INSERT is ON.
您没有使用列列表。指定列。
如错误所述,您需要一个列列表。
INSERT INTO UserGroups (Id, GroupHeadId)
VALUES (0,0)