2016-04-29 24 views
0

我有两个表TRANSACTION和PROCESS。我需要在流程表中存储每个商店的总交易量,并且一旦处理完事务,TRANSACTION表中的processId应该被更新。PL/SQL同时将总和值插入到dest表和更新源表中

表:交易

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伪列在同一事务的下一个插入中引用它,该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工作的例子。

支持数据库对象:

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; 

处理与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; 
/

实施例运行:

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>