Skip to content
Advertisement

Cannot insert the value NULL into column

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.

Advertisement

Answer

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).

Advertisement