无法将 NULL 值插入列中

发布于 2024-12-05 10:08:00 字数 1281 浏览 1 评论 0原文

我遇到了一些问题。我试图更新一张表,但它没有更新,因为其中一个字段包含具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一腔孤↑勇 2024-12-12 10:08:00

问题不在于 nextUpdate 列;根据错误消息,问题是尝试将 NULL 分配到 quantityLimit 列中。查看您的代码,您将 quantityLimit 设置为等于值 allocation,我认为它是数据库中的另一列 - 确保该列或输入不为空。

The issue is not the column nextUpdate; according to the error message, the problem is an attempt to assign NULL into the column quantityLimit. Looking at your code, you are setting quantityLimit equal to the value allocation, which I presume is another column in your database - make sure that column or input is not null.

风吹短裙飘 2024-12-12 10:08:00

这与 NextUpdate 列为 NULL 无关,这与 empPac 表中的分配列为 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()) OR nextUpdate IS NULL) and empIdent in (select empIdent from employee where custIdent='"&custIdent&"') AND allocation IS NOT 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:

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&"') AND allocation IS NOT NULL"
梦冥 2024-12-12 10:08:00

OR nextUpdate IS NULL 添加到 WHERE 子句拓宽搜索条件 - 即可以找到更多行。

显然,这些附加行之一在 allocation 字段中具有 NULL(然后分配给 quantityLimit,根据错误消息,该字段不是 NULL)。

Adding OR nextUpdate IS NULL to your WHERE 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 to quantityLimit which, according to the error message, is NOT NULL).

装迷糊 2024-12-12 10:08:00

只是一个观察,如果您尝试编辑表列以使其不为空(之前为空)。您可能会收到一条错误消息:

“无法将 null 值插入列更新失败”

即使您已正确使用更新 sql 语句

ALTER TABLE
[dbo].[Table name] 
ALTER COLUMN
  [Column Name]
    NVARCHAR(250) NOT NULL;

解决方案:非常简单

  1. 在同一列的空行上插入一些值
  2. 尝试立即执行更新语句。
    希望这可以节省一些时间并有所帮助。

维努奈尔

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:

"cannot insert the value null into column update fails"

Even though you got the correct update sql statement in place

ALTER TABLE
[dbo].[Table name] 
ALTER COLUMN
  [Column Name]
    NVARCHAR(250) NOT NULL;

Solution: Very simple

  1. Insert some value on the empty rows of the same column
  2. Try to execute update statement now .
    Hope this save some time and would helps.

Vinu Nair

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文