0

我有一个名为[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.
4

1 回答 1

1

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

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

我不确定ProductComponentRelationship.ProductComponentRelationshipID = ProductComponentRelationship.ProductComponentRelationshipID您原始查询中的内容,因此我的可能缺少某些内容。

于 2014-09-17T22:32:58.047 回答