使用另一个表中的数据更新一个表 - Oracle

时间:2012-03-22 15:01:55

标签: sql select oracle10g inner-join

这是我的试用版查询。我正在尝试使用基于person_id的TABLE1的相应数据更新ADDR表。这下面说缺少SET关键字。

UPDATE TABLE1 CFI
INNER JOIN 
(
  SELECT ADDRESS_LINE1, ADDRESS_LINE2, CITY, STATE_PROVINCE, ZIP FROM ADDR WHERE PERSON_ID = CFI.PERSON_ID AND STATUS = 'Active' 
) AS ADDR_DATA
SET CFI.PAYEE_ADDRESS1 = ADDR_DATA.ADDRESS_LINE1, 
    CFI.PAYEE_ADDRESS2 = ADDR_DATA.ADDRESS_LINE2, 
    CFI.PAYEE_CITY= ADDR_DATA.CITY, 
    CFI.PAYEE_STATE_PROVINCE = ADDR_DATA.STATE_PROVINCE,
    CFI.PAYEE_POSTALCODE = ADDR_DATA.ZIP, 
    CFI.PAYEE_COUNTRY_CODE = 'USA'    
WHERE CFI.CUSTOMER_INSTITUTION_ID = 966362 AND CFI.PERSON_ID = ADDR_DATA.PERSON_ID;

请帮忙。提前谢谢。

2 个答案:

答案 0 :(得分:2)

那将是

UPDATE TABLE1 CFI
    SET (PAYEE_ADDRESS1, PAYEE_ADDRESS2, PAYEE_CITY, PAYEE_STATE_PROVINCE, PAYEE_POSTALCODE, PAYEE_COUNTRY_CODE) =
        (SELECT ADDRESS_LINE1, ADDRESS_LINE2, CITY, STATE_PROVINCE, ZIP, 'USA' FROM ADDR WHERE PERSON_ID = CFI.PERSON_ID AND STATUS = 'Active')
    WHERE CFI.CUSTOMER_INSTITUTION_ID = 966362;

编辑:修复了错误(希望如此)

答案 1 :(得分:1)

MERGE INTO TABLE1
USING (
       SELECT PERSON_ID, 
              ADDRESS_LINE1, ADDRESS_LINE2, CITY, STATE_PROVINCE, ZIP 
         FROM ADDR 
        WHERE STATUS = 'Active' 
      ) ADDR_DATA
   ON TABLE1.PERSON_ID = ADDR_DATA.PERSON_ID 
      AND TABLE1.CUSTOMER_INSTITUTION_ID = 966362 
WHEN MATCHED THEN 
   UPDATE
      SET PAYEE_ADDRESS1 = ADDR_DATA.ADDRESS_LINE1, 
          PAYEE_ADDRESS2 = ADDR_DATA.ADDRESS_LINE2, 
          PAYEE_CITY = ADDR_DATA.CITY, 
          PAYEE_STATE_PROVINCE = ADDR_DATA.STATE_PROVINCE,
          PAYEE_POSTALCODE = ADDR_DATA.ZIP, 
          PAYEE_COUNTRY_CODE = 'USA';