更新声明失败

时间:2014-09-17 22:26:54

标签: sql msg

我有一个名为[dbo].[ProductComponentRelationship]的表,有4个字段

[ProductComponentRelationshipID] PK, INt, Not Null
[ProductItemID] FK, Int Not Null
[ComponentItemID] FK, Int, Not Null
[SequenceNumber] int null

此表包含一堆值。我需要更新上表中的4000条记录。因此,我使用productItemID和新的ComponentitemID值填充了一个单独的表。我尝试在下面运行sql语句,但失败了:

update ProductComponentRelationship set ComponentItemID = 
(select compid from cst_pricefix where 
ProductComponentRelationship.ProductItemID = cst_pricefix.prditem and 
ProductComponentRelationship.ProductComponentRelationshipID = ProductComponentRelationship.ProductComponentRelationshipID )

Error Message: 
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'ComponentItemID', table 'SDSDB.dbo.ProductComponentRelationship'; column does not allow nulls. UPDATE fails.

1 个答案:

答案 0 :(得分:1)

如果这是从dbo看起来的SQL Server,您可以通过这样的连接进行更新:

update
    pcr
set
    ComponentItemID = f.compid
from
    ProductCompnentRelationship pcr
        inner join
    cst_pricefix f
        on pcr.ProductItemID = f.prditem

我不确定原始查询中的ProductComponentRelationship.ProductComponentRelationshipID = ProductComponentRelationship.ProductComponentRelationshipID是什么,所以我的内容可能会遗漏。

相关问题