有將近兩萬筆的畫展資料須歸類在對應的畫展名稱中,資料都是excel檔,因此先將資料imp到oracle中利用sql developer
原始資料如下:
為了將畫展資料表(abc)中的資料有對應的名稱因此先新增一個column(name1)來儲存資料
SQL> alter table abc
add name1 varchar2(100);
select * from draw; (畫展名稱表)
select * from abc; (畫展資料表)
再來就是進行資料ID的比對,將比對出一樣ID的畫展名稱更新到abc表中的column(name1)
PLSQL如下:
*****************************
begin
for rec in(select id,name from draw) loop //利用update語法+for迴圈
update abc
set name1=rec.name //更新資料進column(name1)
where id=rec.id ; //資料id比對
end loop;
end;
/
如果沒有對應的ID就在column(name1)中打上'XX' 表示資料不存在在此畫展中
SQL>update draw1 set name1='XX' where name1 is null;
*****************************
結果將資料全部更新了,再將資料表exp匯出如下:
END
小提醒: 如果不想要UPDATE後A表不在B表的欄位變成NULL 則要加上
where exists (select X from b where b.欄位=a.欄位);
