我的UPDATE语句中的逻辑有什么问题?

时间:2010-04-10 16:12:34

标签: sql-server inner-join

我很感激UPDATE声明提供了一些帮助。

我想使用tblOrderHeadtblCustomer对应参数intDocumentNo的内容更新@intDocumentNo。但是当我运行my语句时,订单表只会使用customer表第一行​​的内容进行更新。

我的逻辑有什么问题?

我使用的是Microsoft SQL Server。

谢谢,

的Stefan

UPDATE      dbo.tblOrderHead
SET         dbo.tblOrderHead.intCustomerNo   = @intCustomerNo ,
            dbo.tblOrderHead.intPaymentCode  = dbo.tblCustomer.intPaymentCode,
            dbo.tblOrderHead.txtDeliveryCode = dbo.tblCustomer.txtDeliveryCode,
            dbo.tblOrderHead.txtRegionCode   = dbo.tblCustomer.txtRegionCode,
            dbo.tblOrderHead.txtCurrencyCode = dbo.tblCustomer.txtCurrencyCode,
            dbo.tblOrderHead.txtLanguageCode = dbo.tblCustomer.txtLanguageCode
FROM        dbo.tblOrderHead
INNER JOIN  dbo.tblCustomer ON dbo.tblOrderHead.intOrderNo = @intDocumentNo

解决方案

如果有人和我一样愚蠢,那就是同样的事情,这就是你解决问题的方法:

UPDATE      dbo.tblOrderHead
SET         dbo.tblOrderHead.intCustomerNo   = @intCustomerNo ,
            dbo.tblOrderHead.intPaymentCode  = dbo.tblCustomer.intPaymentCode,
            dbo.tblOrderHead.txtDeliveryCode = dbo.tblCustomer.txtDeliveryCode,
            dbo.tblOrderHead.txtRegionCode   = dbo.tblCustomer.txtRegionCode,
            dbo.tblOrderHead.txtCurrencyCode = dbo.tblCustomer.txtCurrencyCode,
            dbo.tblOrderHead.txtLanguageCode = dbo.tblCustomer.txtLanguageCode
FROM        dbo.tblOrderHead
INNER JOIN  dbo.tblCustomer ON dbo.tblOrderHead.intCustomerNo = dbo.tblCustomer.intCustomerNo
AND         dbo.tblOrderHead.intOrderNo = @intDocumentNo

1 个答案:

答案 0 :(得分:0)

问题在于,您没有指定需要加入2个表tblOrderHeadtblCustomer的条件!

你需要像

这样的东西
INNER JOIN  dbo.tblCustomer 
ON dbo.tblOrderHead.someColumn = dbo.tblCustomer.someColumn
and dbo.tblOrderHead.intOrderNo = @intDocumentNo