2012-03-22 167 views
1

這是我的試用查詢。我正在嘗試根據person_id更新TABLE1表以及來自ADDR的相應數據。這下面說失蹤的SET關鍵字。使用另一個表中的數據更新一個表 - Oracle

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; 

請大家幫忙。提前致謝。

回答

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'; 
+0

感謝您的回覆。但它說缺少ON關鍵字。 – Vaandu 2012-03-22 15:52:49

+0

嘗試從ADDR_DATA子查詢中選擇'PERSON_ID',它工作。非常感謝。 – Vaandu 2012-03-22 16:06:12

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; 

編輯:錯誤修復(希望)

+0

感謝您的答覆。但它說,ADDR_DATA.PERSON_ID無效標識符。 – Vaandu 2012-03-22 15:15:37

+0

Vanathi:現在應該工作,以防萬一它重要 – 2012-03-22 20:02:17

相關問題