94

I'm using the following query:

INSERT INTO role (name, created) VALUES ('Content Coordinator', GETDATE()), ('Content Viewer', GETDATE())

However, I'm not specifying the primary key (which is id). So my questions is, why is sql server coming back with this error:

Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'id', table 'CMT_DEV.dbo.role'; column does not allow nulls. INSERT fails.
The statement has been terminated.
James Drinkard
  • 15,342
  • 16
  • 114
  • 137
Ben
  • 60,438
  • 111
  • 314
  • 488

11 Answers11

173

I'm assuming that id is supposed to be an incrementing value.

You need to set this, or else if you have a non-nullable column, with no default value, if you provide no value it will error.

To set up auto-increment in SQL Server Management Studio:

  • Open your table in Design
  • Select your column and go to Column Properties
  • Under Indentity Specification, set (Is Identity)=Yes and Indentity Increment=1
Curtis
  • 101,612
  • 66
  • 270
  • 352
  • but i have 2 column called id and id_student.. I same above error.. i can set identity only one column..if i set for id means, error for id_student or if i set for id_student means got error for id. – pcs Nov 24 '15 at 12:35
  • When trying to save these modifications, Management Studio says "Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created." – Henno Aug 25 '16 at 11:18
  • 10
    Even though I did not enable this option, it turned out to be ON. I unchecked it (under Tools > Options > Designers) and was able to save the changes. – Henno Aug 25 '16 at 11:26
11

use IDENTITY(1,1) while creating the table eg

CREATE TABLE SAMPLE(
[Id]     [int]  IDENTITY(1,1) NOT NULL,
[Status] [smallint] NOT NULL,

CONSTRAINT [PK_SAMPLE] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)
)
Nick
  • 1,178
  • 3
  • 24
  • 36
Minakshi Korad
  • 111
  • 1
  • 4
4

If the id column has no default value, but has NOT NULL constraint, then you have to provide a value yourself

INSERT INTO dbo.role (id, name, created) VALUES ('something', 'Content Coordinator', GETDATE()), ('Content Viewer', GETDATE())
Andy Irving
  • 2,657
  • 1
  • 14
  • 11
2

Encountered the same issue. This is something to do with your table creation. When you created table you have not indicate 'ID' column to be Auto Increment hence you get this error. By making the column Primary Key it cannot be null or contain duplicates hence without Auto Increment pretty obvious to throw column does not allow nulls. INSERT fails.

There are two ways you could fix this issue.

1). via MS SQL Server Management Studio

  1. Got to MS SQL Server Management Studio

  2. Locate your table and right click and select Design

  3. Locate your column and go to Column Properties

  4. Under Indentity Specification: set (Is Identity)=Yes and Indentity Increment=1

2). via ALTER SQLs

ALTER TABLE table DROP COLUMN id; // drop the existing ID
ALTER TABLE table ADD id int IDENTITY(1, 1) NOT NULL; // add new column ID with auto-increment
ALTER TABLE table ADD CONSTRAINT PK_ident_test PRIMARY KEY CLUSTERED (id); // make it primary key
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
1

WARNING! Make sure the target table is locked when using this method (As per @OnurOmer's comment)

if you can't or don't want to set the autoincrement property of the id, you can set value for the id for each row like this:

INSERT INTO role (id, name, created)
SELECT 
      (select max(id) from role) + ROW_NUMBER() OVER (ORDER BY name)
    , name
    , created
FROM (
    VALUES 
      ('Content Coordinator', GETDATE())
    , ('Content Viewer', GETDATE())
) AS x(name, created)
robotik
  • 1,837
  • 1
  • 20
  • 26
  • 1
    If the table during insertion of data isn't locked, you'll end up with duplicates! A terrible idea... There are better ways of doing this, have a look at here: https://stackoverflow.com/a/37370430/2015558 – Onur Omer Jun 27 '22 at 15:18
  • 1
    @OnurOmer it is locked by default, but I added a warning in my answer, so users can be aware – robotik Jul 04 '22 at 09:29
0

You either need to specify an ID in the insert, or you need to configure the id column in the database to have Identity Specification = Yes.

JupiterP5
  • 318
  • 1
  • 10
0

As id is PK it MUST be unique and not null. If you do not mention any field in the fields list for insert it'll be supposed to be null or default value. Set identity (i.e. autoincrement) for this field if you do not want to set it manualy every time.

quzary
  • 285
  • 1
  • 4
0

You need to set autoincrement property of id column to true when you create the table or you can alter your existing table to do this.

0

you didn't give a value for id. Try this :

INSERT INTO role (id, name, created) VALUES ('example1','Content Coordinator', GETDATE()), ('example2', 'Content Viewer', GETDATE())

Or you can set the auto increment on id field, if you need the id value added automatically.

natadecoco
  • 83
  • 13
0

I had a similar problem and upon looking into it, it was simply a field in the actual table missing id (id was empty/null) - meaning when you try to make the id field the primary key it will result in error because the table contains a row with null value for the primary key.

This could be the fix if you see a temp table associated with the error. I was using SQL Server Management Studio.

vid.dev
  • 9
  • 1
0

RULE: You cannot IGNORE those colums that do not allow null values, when inserting new data.

Your Case

  • You're trying to insert values, while ignoring the id column, which does not allow nulls. Obviously this won't work.
  • Gladly for you the "Identity Specification" seems to automatically fill the not nullable id values for you (see selected answer), when you later execute the insert query.

My Case

  • The problem (while using SSMS): I was having this error when trying to add a new non-nullable column to an already existing table with data. The error I'd got was:

Cannot insert the value NULL into column 'id_foreign', table 'MyDataBase.dbo.Tmp_ThisTable'; column does not allow nulls. INSERT fails. The statement has been terminated.

  • The solution:
    1. I created the column I needed id_foreign, allowing nulls.
    2. I edited/inserted all the required values for id_foreign.
    3. Once the values where in place, I went back and unchecked the "Allow Nulls" checkbox. Now the error was gone.
carloswm85
  • 1,396
  • 13
  • 23