1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| SQL> create table testt (Libe varchar2(10), col1 number, col2 number, col3 number)
2 /
Table created.
SQL> create table testt_source (Libe varchar2(10), test1 number, test2 number)
2 /
Table created.
SQL> insert into testt_source values ('col1',1,2);
1 row created.
SQL> insert into testt_source values ('col2',3,4);
1 row created.
SQL> insert into testt_source values ('col3',7,8);
1 row created.
SQL>
SQL> insert into testt (libe) values ('test1');
1 row created.
SQL> insert into testt (libe) values ('test2');
1 row created.
SQL>
SQL> commit;
Commit complete.
SQL>
SQL> select * from testt_source;
LIBE TEST1 TEST2
---------- ---------- ----------
col1 1 2
col2 3 4
col3 7 8
SQL> select * from testt;
LIBE COL1 COL2 COL3
---------- ---------- ---------- ----------
test1
test2
SQL>
SQL> merge INTO testt t
2 USING (SELECT new_libe,
3 max(case when libe = 'col1' then new_val end) AS col1,
4 max(case when libe = 'col2' then new_val end) AS col2,
5 max(case when libe = 'col3' then new_val end) AS col3
6 FROM (SELECT s.libe,
7 case when p.rn = 1 then 'test1'
8 when p.rn = 2 then 'test2'
9 end AS new_libe,
10 case when p.rn = 1 then s.test1
11 when p.rn = 2 then s.test2
12 end AS new_val
13 FROM testt_source s
14 CROSS JOIN (SELECT rownum AS rn FROM dual connect BY level <= 2) p
15 )
16 GROUP BY new_libe
17 ) u
18 ON (t.libe = u.new_libe)
19 when matched then
20 UPDATE SET t.col1 = u.col1,
21 t.col2 = u.col2,
22 t.col3 = u.col3
23 /
2 rows merged.
SQL>
SQL> select * from testt;
LIBE COL1 COL2 COL3
---------- ---------- ---------- ----------
test1 1 3 7
test2 2 4 8
SQL> |
Partager