How can I calculate aggregate affected rows if there are Multiple DML query in my PLSQL block?

前端 未结 1 1244
自闭症患者
自闭症患者 2021-01-23 14:36

I have a scenario where there may exist multiple DML statements inside my PLSQL Block, I was looking for some generic approach by using which I can calculate total no of rows af

相关标签:
1条回答
  • 2021-01-23 15:00

    You may log the counts in a generic logging table using a generic procedure.

    Logging table

    CREATE TABLE dml_logs (
         log_id      NUMBER PRIMARY KEY,
         step        VARCHAR2(200),
         row_count   NUMBER,
         log_date    DATE
    );
    

    Sequence for id

    create sequence seq_dml_logs ;
    

    Logging procedure

    CREATE OR REPLACE PROCEDURE log_dml (
         p_step        VARCHAR2,
         p_row_count   NUMBER,
         p_log_date    DATE
    ) IS
         PRAGMA autonomous_transaction;
    BEGIN
         INSERT INTO dml_logs (
              log_id,
              step,
              row_count,
              log_date
         ) VALUES (
              seq_dml_logs.NEXTVAL,
              p_step,
              p_row_count,
              p_log_date
         );
         COMMIT;
    END;
    /
    

    PL/SQL block with DML

    DECLARE
    v_step dml_logs.step%TYPE;
    BEGIN
    
      v_step := 'cust_temp_a_update';
       UPDATE cust_temp_a SET name = 'new_val' WHERE id = 10;
    
     log_dml(v_step,SQL%ROWCOUNT,SYSDATE);
    
     v_step := 'cust_temp_b_update';
       UPDATE cust_temp_b SET name = 'new_val' WHERE id = 20;
    
     log_dml(v_step,SQL%ROWCOUNT,SYSDATE);
    END;
    /
    

    Then, aggregation is simple.

    select SUM(row_count) FROM dml_logs 
    where step = ? and log_date = ? -- all the required conditions.
    

    In order to better identify that the records belong to a particular run or a batch, you may add another column in the dml_logs called batch_number . Log this number to identify unique runs of your dmls and your query to get the aggregate details become much simpler.

    0 讨论(0)
提交回复
热议问题