IDENTITY_INSERT SQL script

850 views Asked by At

Situation

I have a SQL script that I need to push some basic data to my database. Therefore I use the following script (and others). I want to provide manually an primary key for the rows I manually create.

Problem

If I execute the script it will say that I need to enable IDENTITY_INSERT. I added this row SET IDENTITY_INSERT UserGroups ON; as many examples use but it still will give the same error when I have added it.

SET IDENTITY_INSERT UserGroups ON;  
GO 

INSERT INTO UserGroups VALUES (0, 0);

Error when running script:

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.

Question

Do I need to change something in my database as well or is there something else what I forget in the script to add manually a primary key?

Details

I use SQL Server 2016 Management Studio.

I use DDL Script for SQL scripts

I work with Entity Framework.

I got two columns in this table

  1. Primary key: Id
  2. int: GroupHeadId
2

There are 2 answers

0
Jacob H On BEST ANSWER

As the error states, you need a column list.

INSERT INTO UserGroups (Id, GroupHeadId)
VALUES (0,0)
1
Daniel Mann On

The error message is telling you the problem.

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.

You're not using a column list. Specify the columns.