我正在使用以下查询:
INSERT INTO role (name, created) VALUES ('Content Coordinator', GETDATE()), ('Content Viewer', GETDATE())
但是,我没有指定主键(即 id
)。所以我的问题是,为什么 sql server 会出现这个错误:
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.
原文由 Ben 发布,翻译遵循 CC BY-SA 4.0 许可协议
我假设
id
应该是一个递增值。你需要设置这个,否则如果你有一个不可为空的列,没有默认值,如果你没有提供值,它会出错。
在 SQL Server Management Studio 中设置自动增量:
Design
中打开你的表Column Properties
Indentity Specification
下,设置(Is Identity)=Yes
和Indentity Increment=1