2013-04-27 64 views
0

我想根據我的外部表更新員工表更新表,但我得到ORA-01427錯誤, 單行子查詢返回不止一行如何從外部表

employee(emp_id, emp_name, job_history, city_code) 
ext_table(emp_name, job_history, city_name) 
city(city_code, city_name) 

數據在我ext_table如下:

Sandy, waitress, los angeles 
Sandy, restaurant manager, los angeles 
John, store manager, phoenix 

update employee em 
set (em.emp_name, em.job_history, em.city_code) = 
    (select t.emp_name, t.job_history, t.city_code 
    from (select distinct(emp_name), job_history, c.city_code from 
      ext_table e, city c where e.city_name=c.city_name) t) 
    where em.emp_name=t.emp_name; 

我非常感謝所有幫助

回答

1

這是MERGE是:

merge into employee 
using 
(
    select e.emp_name, e.job_history, c.city_code 
    from ext_table e 
    join city c on e.city_name=c.city_name 
) t on (t.emp_name = employee.emp_name) 
when matched then update 
    set job_history = t.job_history, 
     city_code = t.city_code; 

請注意,這是沒用的,更新emp_name,因爲這是你使用ext_tableemployee之間加入列。

上面假定emp_name在ext_table(和員工)中是唯一的。如果不是這種情況,您需要找到一些能在外部表中唯一標識員工的「鑰匙」。

另外:distinct不是功能。

select distinct (foo), barselect distinct foo, bar完全相同。它始終在全部列上運行。兩者之間的差異與​​和select foo, bar之間的差異相同。

-2

你可以通過使用更新查詢加入。檢查以下代碼

UPDATE SET僱員JOB_HISTORY = B.job_history,CITY_CODE = C.city_code FROM EMPLOYEE爲A INNER JOIN ext_table AS B開 A.emp_name = B.emp_name INNER JOIN城市AS C ON B. city_name = C.city_name

+0

這對Oracle無效。 – 2013-04-27 22:27:05

0

另一種可能的選擇是將外部錶轉換爲常規表並更新。要將外部轉換爲常規表,請使用SQL Developer。步驟:1.創建與外部桌子結構相同的空桌子。將數據從外部輸出到註冊表。表。該過程類似於將數據從文件導出到Excel。