我遇到了一些问题。我试图获取一个表来更新,但它没有更新,因为其中一个字段包含具有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&"')"
现在,下次更新列可能为空,所以我想以适应与此查询:
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”,以及nextUpdate的其他条件。
但是,我收到一个错误“无法将值NULL插入列'quantityLimit',表'myname.dbo.EmpPac';列不允许为空,UPDATE失败。
这对我来说没有任何意义,因为在查看myLittleAdmin时,会为该表列中的某些行显示NULL值。
您应该真正参数化您的查询http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html –