Create a trigger to copy data to an audit table at runtime

For example, the AR Statement Generation program inserts data into two tables (AR_STATEMENT_HEADERS and AR_STATEMENT_LINE_CLUSTERS), reports from this data, then clears down the tables when it finishes.

Ordinarily the use of database triggers should be kept to an absolute minimum, but in this case they were used for debugging purposes.

In this example, how do you find out what data has been inserted into AR_STATEMENT_LINE_CLUSTERS?

First create a backup table:

CREATE TABLE ar_statement_line_clusters_bak
AS
(
SELECT *
FROM   ar_statement_line_clusters
)

Then create the database trigger (you can get the column names from the all_tab_columns table):

CREATE OR REPLACE TRIGGER ar_stmt_line_clust_after_ins
 AFTER INSERT
 ON ar_statement_line_clusters
 FOR EACH ROW
 BEGIN
     INSERT INTO ar_statement_line_clusters_bak
 (due_date
 ,transaction
 ,trx_date
 ,invoice_number
 ,general_sequence
 ,trx_order_sequence
 ,payment_schedule_sequence
 ,customer_trx_sequence
 ,line_cluster_id
 ,amount_applied_from
 ,receipt_currency_code
 ,transaction_code
 ,orig_payment_amount
 ,bill_to_city
 ,site_use_id
 ,request_id
 ,receivable_application_id
 ,customer_id
 ,adjustment_id
 ,amount_due
 ,trx_amount
 ,bill_to_location
 ,reference
 )
 VALUES
 (:new.due_date
 ,:new.transaction
 ,:new.trx_date
 ,:new.invoice_number
 ,:new.general_sequence
 ,:new.trx_order_sequence
 ,:new.payment_schedule_sequence
 ,:new.customer_trx_sequence
 ,:new.line_cluster_id
 ,:new.amount_applied_from
 ,:new.receipt_currency_code
 ,:new.transaction_code
 ,:new.orig_payment_amount
 ,:new.bill_to_city
 ,:new.site_use_id
 ,:new.request_id
 ,:new.receivable_application_id
 ,:new.customer_id
 ,:new.adjustment_id
 ,:new.amount_due
 ,:new.trx_amount
 ,:new.bill_to_location
 ,:new.reference
 );

END;

Don’t forget to drop the trigger when you’re finished:

DROP TRIGGER ar_stmt_line_clust_after_ins
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s