2016-04-29 9 views
0

私は2つのテーブルTRANSACTIONとPROCESSを持っています。各ショップの合計取引金額をプロセステーブルに保存し、トランザクションが処理されたらTRANSACTIONテーブルのprocessIdを更新する必要があります。PL/SQLは、destテーブルと更新ソーステーブルに同時に合計値を挿入します

表:TRANSACTION

enter image description here

表:PROCESS

しかしenter image description here

、現在私は、プロセステーブルに値を挿入することができる:

BEGIN 
    INSERT INTO process 
    (processId, shopId, totalTransactionAmount) 
    SELECT my_seq.NEXTVAL, shop, total 
    FROM 
     (SELECT shopId shop, sum(transactionAmount) total 
     FROM transaction 
     GROUP BY shopId); 
END; 

しかし、どのようにトランザクションが処理されると、トランザクションテーブルをprocessIdで更新できますか?

答えて

0

挿入内でmy_sql.nextvalを呼び出した後、CURRVAL疑似列によって同じトランザクションの次の挿入時に参照することができます。これにより、現行の順序値が生成されます。だから、のようなもの:

BEGIN 
INSERT INTO process 
(processId, shopId, totalTransactionAmount) 
SELECT my_seq.NEXTVAL, shop, total 
FROM 
(SELECT shopId shop, sum(transactionAmount) total 
FROM transaction 
GROUP BY shopId); 

**insert into transaction (..) values (my_seq.currval, ....** 
END; 
1

これはforallbulk 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> 
関連する問題