これはforall
とbulk collect
を使用して実施例です。
サポートするデータベースオブジェクト:PL/SQLでの
create table transaction (
transaction_id number
,shop_id varchar2(2)
,transaction_amount number
,process_id number
);
insert all
into transaction values(1, 's1', 10, null)
into transaction values(2, 's2', 8, null)
into transaction values(3, 's1', 5, null)
into transaction values(4, 's2', 15, null)
select 1 from dual;
create table process (
process_id number
,shop_id varchar2(2)
,total_transaction_amount number
);
create sequence process_id_s start with 2016042900;
処理:
declare
-- required supporting data structures
type transaction_t is record(
shop_id varchar2(2)
,total_amount number
);
type transaction_list_t is table of transaction_t;
type process_t is record(
process_id number
,shop_id varchar2(2)
);
type process_list_t is table of process_t;
v_transactions transaction_list_t;
v_processes process_list_t;
begin
-- collect transaction data to PL/SQL data structure
select shop_id, sum(transaction_amount)
bulk collect into v_transactions
from transaction
where process_id is null
group by shop_id
;
-- insert transaction data to process-table and collect process and shop ids
-- to PL/SQL data structure
forall i in v_transactions.first .. v_transactions.last
insert into process(process_id, shop_id, total_transaction_amount)
values(process_id_s.nextval, v_transactions(i).shop_id, v_transactions(i).total_amount)
returning process_id, shop_id bulk collect into v_processes
;
-- update process id to transaction-table
forall i in v_processes.first .. v_processes.last
update transaction set
process_id = v_processes(i).process_id
where shop_id = v_processes(i).shop_id
;
end;
/
例を実行します。
SQL> @so55
Table created.
4 rows created.
TRANSACTION_ID SH TRANSACTION_AMOUNT PROCESS_ID
-------------- -- ------------------ ----------
1 s1 10
2 s2 8
3 s1 5
4 s2 15
Table created.
Sequence created.
PL/SQL procedure successfully completed.
PROCESS_ID SH TOTAL_TRANSACTION_AMOUNT
---------- -- ------------------------
2016042900 s2 23
2016042901 s1 15
TRANSACTION_ID SH TRANSACTION_AMOUNT PROCESS_ID
-------------- -- ------------------ ----------
1 s1 10 2016042901
2 s2 8 2016042900
3 s1 5 2016042901
4 s2 15 2016042900
SQL>