简体   繁体   English

SQL 服务器:无法插入默认列值

[英]SQL Server: Cannot Insert Default Column Value

I've created a table like this我创建了一个这样的表

CREATE TABLE dbo.[Users] 
(
    [UserID_PK] INT IDENTITY(1, 1) PRIMARY KEY NOT NULL,
    [Email] NVARCHAR(60) NOT NULL,
    [Username] NVARCHAR(15) NOT NULL,
    [Password] NVARCHAR(24) NOT NULL,
    [Salt] NVARCHAR(24) NOT NULL,
    [Token] NVARCHAR(200),
    [DateCreated] DATETIME DEFAULT (GETDATE()) NOT NULL
);

Every time my API creates a record, it returns this message.每次我的 API 创建记录时,它都会返回此消息。 I am not sure why the default value isn't being inserted instead of NULL. I've tried GETDATE() and CURRENT_TIMESTAMP :我不确定为什么没有插入默认值而不是 NULL。我试过GETDATE()CURRENT_TIMESTAMP

Cannot insert the value NULL into column 'DateCreated';无法将值 NULL 插入“DateCreated”列; column does not allow nulls.列不允许空值。 INSERT fails.插入失败。

The root cause: The problem is not with how you defined default for the DateCreated column on the table it's with your insert statement.根本原因:问题不在于您如何使用插入语句为表上的DateCreated列定义default

How to implicitly use default value: If you want the default value to be used make sure to not include the column(s) defined with default in the insert statement your using.如何隐式使用默认值:如果您希望使用default值,请确保不要在您使用的insert语句中包含使用default值定义的列。 Example: insert into dbo.Users(Name) select 'someName' Your column(s) defined with default will get the default value implicitly if you don't explicitly include it as a column in the insert statement when inserting row(s).示例: insert into dbo.Users(Name) select 'someName'如果您在插入行时未明确将其作为insert语句中的列包含,则使用default定义的列将隐式获得默认值。

Why you are getting the error: You are getting this Error:为什么会收到错误:您收到此错误:

Cannot insert the value NULL into column 'DateCreated';无法将值 NULL 插入“DateCreated”列; column does not allow nulls.列不允许空值。 INSERT fails.插入失败。

because you're explicitly including the DateCreated column in your insert statement and not providing a value, something like: insert into dbo.Users(Email,DateCreated) select 'someName'因为您在插入语句中明确包含DateCreated列并且没有提供值,例如: insert into dbo.Users(Email,DateCreated) select 'someName'

Resolution: To resolve this provide a value for the column like this: insert into dbo.Users(Email,DateCreated) select 'someEmail',getdate() or remove DateCreated from the insert and it will implicitly get the default value even if you don't include it as a column when you insert values for other columns on the row.解决方案:要解决此问题,请像这样为列提供值: insert into dbo.Users(Email,DateCreated) select 'someEmail',getdate()或从插入中删除DateCreated ,即使您不这样做,它也会隐式获取默认值' 当您为该行的其他列插入值时,将其作为列包括在内。 You can even hard code a string literal date value '2020-08-22 19:05:00' to set a specific date time if you don't want to use getdate() or another similar date function.如果您不想使用getdate()或其他类似日期 function,您甚至可以硬编码字符串文字日期值'2020-08-22 19:05:00'以设置特定日期时间。

In SQL Server, you have two options for inserting a default value.在 SQL 服务器中,您有两种插入默认值的选项。 One is to leave out the column:一种是省略该列:

insert into users (email, username, password, salt, token)
    values (?, ?, ?, ?, ?);

The second is to use the default keyword:第二种是使用default关键字:

insert into users (email, username, password, salt, token, dateCreated)
    values (?, ?, ?, ?, ?, default);

NULL is not a synonym for DEFAULT . NULL不是DEFAULT的同义词。

I should point out that there is actually a third way.我应该指出实际上还有第三种方式。 That requires navigating the metadata.tables to look up the default value for a column.这需要导航 metadata.tables 以查找列的默认值。 As a general rule, that approach is not useful.作为一般规则,这种方法是没有用的。 On occasion, it can be quite helpful.有时,它会很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM