我遇到了一些問題。我試圖獲取一個表來更新,但它沒有更新,因爲其中一個字段包含具有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 –