0

我正在尝试更新表,但我的问题是目标表有重复记录,因此我的更新因此失败。这是错误:尝试使用来自多个连接行的值更新目标行。我知道在更新表时,我们必须加入唯一键,但我无法从表中删除重复项,所以我正在为我的情况寻找解决方法。CUSTOMERTABLE 是具有重复项的那个。这是我的查询:

UPDATE CUSTOMERTABLE
SET SERVICE = 'BILLING'
FROM
(SELECT distinct(CUSTOMER_ID)AS ACCT_ID
       ,ED.CUSTOMER_NAME
       , ED.CUSTOMER_ADDRESS
  FROM CUSTOMER_RELATION ED, STG_CUSTOMER_REV TXN
  WHERE ED.CUSTOMER_ID = TXN.CUS_ID
 )AS X

WHERE X.ACCT_ID = CUSTOMERTABLE.ACCOUNT_NUMBER; 
4

3 回答 3

2

试着用一个IN子句来写它:

UPDATE CUSTOMERTABLE
SET SERVICE = 'BILLING'
WHERE CUSTOMERTABLE.ACCOUNT_NUMBER IN
    (SELECT CUSTOMER_ID
     FROM CUSTOMER_RELATION ED
     JOIN STG_CUSTOMER_REV TXN ON ED.CUSTOMER_ID = TXN.CUS_ID)
于 2013-11-03T16:10:12.323 回答
2

这是另一种选择,与大型表的IN解决方案相比,它可能具有更好的性能。CUSTOMER_RELATIONSTG_CUSTOMER_REV

UPDATE C
   SET SERVICE = 'BILLING'
FROM CUSTOMERTABLE C
WHERE EXISTS (SELECT 1 
              FROM CUSTOMER_RELATION ED, STG_CUSTOMER_REV TXN
              WHERE ED.CUSTOMER_ID = TXN.CUS_ID AND CUSTOMER_ID = C.ACCOUNT_NUMBER);
于 2013-11-03T16:11:31.090 回答
0

尝试对 CustomerId 进行分组

UPDATE CUSTOMERTABLE
SET SERVICE = 'BILLING'
FROM
(SELECT distinct(CUSTOMER_ID)AS ACCT_ID
       ,ED.CUSTOMER_NAME
       , ED.CUSTOMER_ADDRESS
  FROM CUSTOMER_RELATION ED, STG_CUSTOMER_REV TXN
  WHERE ED.CUSTOMER_ID = TXN.CUS_ID
  GROUP BY ED.CUSTOMER_ID
 )AS X

WHERE X.ACCT_ID = CUSTOMERTABLE.ACCOUNT_NUMBER; 

您需要确保您的选择返回非重复项。尝试在没有更新语句的情况下使用该选择,并检查该选择是否包含您想要摆脱的重复项。

于 2013-11-03T16:07:44.480 回答