Why is SQL server throwing this error: Cannot insert the value NULL into column 'id'?

前端 未结 13 1517
鱼传尺愫
鱼传尺愫 2020-11-27 05:35

I\'m using the following query:

INSERT INTO role (name, created) VALUES (\'Content Coordinator\', GETDATE()), (\'Content Viewer\', GETDATE())
13条回答
  •  孤街浪徒
    2020-11-27 06:29

    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)
    

提交回复
热议问题