2016-04-29 8 views
0

Ich habe zwei Tabellen TRANSACTION und PROCESS. Ich muss den gesamten Transaktionsbetrag jedes Shops in der Prozesstabelle speichern, UND sobald die Transaktion verarbeitet wurde, sollte die Prozess-ID in der TRANSACTION-Tabelle aktualisiert werden.PL/SQL einfügen Summenwert in Ziel Tabelle und Update Quellentabelle zur gleichen Zeit

Tabelle: TRANSACTION

enter image description here

Tabelle: PROCESS

enter image description here

jedoch zur Zeit kann ich einfügen nur die Werte in die Prozesstabelle:

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; 

Aber wie Kann ich die Transaktionstabelle mit der Prozess-ID aktualisieren, nachdem die Transaktion verarbeitet wurde?

Antwort

0

Nachdem Sie my_sql.nextval in Ihrer Einfügung aufgerufen haben, können Sie in der nächsten Einfügung für die gleiche Transaktion auf die Pseudospalte CURRVAL verweisen, die den aktuellen Sequenzwert gerade generiert hat. So etwas wie:

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

Dies ist ein funktionierendes Beispiel mit forall und bulk collect.

Unterstützung von Datenbankobjekten:

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; 

Verarbeitung mit PL/SQL:

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; 
/

Beispiel laufen:

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> 
Verwandte Themen