无法将 NULL 值插入列中
我遇到了一些问题。我试图更新一张表,但它没有更新,因为其中一个字段包含具有 NULL 值的行。
这是没有错误的原始查询:
sql="UPDATE empPac SET quantityLimit = allocation, allocationStart = '"&allocationStart&"', nextUpdate = DATEADD(mm, allocationMonths, "&allocationStart&"), lastUpdate = GETDATE(), quantityIssued = 0, quantityShipped = 0 WHERE allocationMonths <> 0 AND nextUpdate <= DATEADD(mm, "&checkCondition&", GETDATE()) and empIdent in (select empIdent from employee where custIdent='"&custIdent&"')"
现在,nextUpdate 列可能为 NULL,所以我尝试用此查询来适应这一点:
sql="UPDATE empPac SET quantityLimit = allocation, allocationStart = '"&allocationStart&"', nextUpdate = DATEADD(mm, allocationMonths, "&allocationStart&"), lastUpdate = GETDATE(), quantityIssued = 0, quantityShipped = 0 WHERE allocationMonths <> 0 AND (nextUpdate <= DATEADD(mm, "&checkCondition&", GETDATE()) OR nextUpdate IS NULL) and empIdent in (select empIdent from employee where custIdent='"&custIdent&"')"
您可以看到我在括号中添加了“OR nextUpdate IS NULL”以及其他条件下一个更新。
但是,我收到错误“无法将 NULL 值插入到表“myname.dbo.EmpPac”的列“quantityLimit”中;列不允许为空值。更新失败。”
这对我来说毫无意义,因为在查看 myLittleAdmin 时,该表列中的某些行显示为 NULL 值。
I'm having some problems. I am trying to get a table to update, however it is not updating because one of the fields contains rows with a NULL value.
Heres the original query which gives no error:
sql="UPDATE empPac SET quantityLimit = allocation, allocationStart = '"&allocationStart&"', nextUpdate = DATEADD(mm, allocationMonths, "&allocationStart&"), lastUpdate = GETDATE(), quantityIssued = 0, quantityShipped = 0 WHERE allocationMonths <> 0 AND nextUpdate <= DATEADD(mm, "&checkCondition&", GETDATE()) and empIdent in (select empIdent from employee where custIdent='"&custIdent&"')"
Now, the nextUpdate column might be NULL, so I am trying to accommodate for that with this query:
sql="UPDATE empPac SET quantityLimit = allocation, allocationStart = '"&allocationStart&"', nextUpdate = DATEADD(mm, allocationMonths, "&allocationStart&"), lastUpdate = GETDATE(), quantityIssued = 0, quantityShipped = 0 WHERE allocationMonths <> 0 AND (nextUpdate <= DATEADD(mm, "&checkCondition&", GETDATE()) OR nextUpdate IS NULL) and empIdent in (select empIdent from employee where custIdent='"&custIdent&"')"
You can see I've added "OR nextUpdate IS NULL" in brackets with the other condition for nextUpdate.
However, I am getting an error "Cannot insert the value NULL into column 'quantityLimit', table 'myname.dbo.EmpPac'; column does not allow nulls. UPDATE fails."
This makes no sense to me as, when viewing myLittleAdmin, shows me a NULL value for some rows in that table column.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题不在于
nextUpdate
列;根据错误消息,问题是尝试将NULL
分配到quantityLimit
列中。查看您的代码,您将quantityLimit
设置为等于值allocation
,我认为它是数据库中的另一列 - 确保该列或输入不为空。The issue is not the column
nextUpdate
; according to the error message, the problem is an attempt to assignNULL
into the columnquantityLimit
. Looking at your code, you are settingquantityLimit
equal to the valueallocation
, which I presume is another column in your database - make sure that column or input is not null.这与 NextUpdate 列为 NULL 无关,这与 empPac 表中的分配列为 NULL 有关。处理分配列为 NULL(或修复数据),它将正常工作。
编辑:我唯一能保证的是错误将会消失:
This has nothing to do with the NextUpdate column being NULL, this has to do with the allocation column in empPac table being NULL. Handle the allocation column being NULL (or fix the data) and it will work fine.
EDIT: The only thing I can guarantee with this is that the error will go away:
将
OR nextUpdate IS NULL
添加到WHERE
子句拓宽搜索条件 - 即可以找到更多行。显然,这些附加行之一在
allocation
字段中具有 NULL(然后分配给quantityLimit
,根据错误消息,该字段不是 NULL)。Adding
OR nextUpdate IS NULL
to yourWHERE
clause widens the search criteria - i.e. more rows can be found.Apparently, one of these additional rows has a NULL in
allocation
field (which then gets assigned toquantityLimit
which, according to the error message, is NOT NULL).只是一个观察,如果您尝试编辑表列以使其不为空(之前为空)。您可能会收到一条错误消息:
即使您已正确使用更新 sql 语句
解决方案:非常简单
希望这可以节省一些时间并有所帮助。
维努奈尔
Just an Observation, if you try to Edit a table column to make it Not Null which were null previously . You may get an error saying:
Even though you got the correct update sql statement in place
Solution: Very simple
Hope this save some time and would helps.
Vinu Nair