The stored procedure gives the specified table binding trigger SQL to spell JSON as the storage log

Source: Internet
Author: User

The project gives a new demand, using trigger to generate log table, listen to the database of key table additions and deletions and change operations. Writes the data of the operation into JSON format as a CLOB into Oracle.

1 Create or Replace procedureLogging_messages (tableNamevarchar2 default '%') is2   cursorC_cl is3     SELECTcolumn_name TCL4        fromUser_tab_columns5      WHEREtable_name= Upper(tableName);6 Newjson Clob;7 Oldjson Clob;8V_sqlvarchar2(32000);9V_tmpvarchar2(32000);Ten begin One    forClinchC_CL LOOP A     ifNewjson is NULL  Then -Newjson:= '{"' ||Cl.tcl|| '":""'||'||': NEW.' ||Cl.tcl||'||"'"}'; -     Else theNewjson:=Newjson|| ',' || '{"' ||Cl.tcl|| '":""'||'||': NEW.' ||Cl.tcl||'||"'"}'; -     End if; -     ifOldjson is NULL  Then -Oldjson:= '{"' ||Cl.tcl|| '":""'||'||': Old.' ||Cl.tcl||'||"'"}'; +     Else -Oldjson:=Oldjson|| ',' || '{"' ||Cl.tcl|| '":""'||'||': Old.' ||Cl.tcl||'||"'"}'; +     End if; A   EndLOOP; atDbms_output.put_line ('Newjson:'||Newjson); -Dbms_output.put_line ('Oldjson:'||Oldjson); -V_tmp:=Q'{CREATE OR REPLACE TRIGGER [email protected][email protected] - before INSERT or UPDATE or DELETE on @[email protected] for each ROW - DECLARE - JSON Clob; in BEGIN - IF INSERTING Then to JSON: ='@newjson@'; + Dbms_output.put_line ('The JSON string is insert:'|| JSON); - INSERT into ddllog values ('@TABLE_NAME@','Insert', json,sysdate); the End If; *      $ IF DELETING ThenPanax Notoginseng JSON: ='@oldjson@'; - Dbms_output.put_line ('The JSON string is delete:'|| JSON); the INSERT into ddllog values ('@TABLE_NAME@','Delete', json,sysdate); + End If; A      the IF UPDATING Then + JSON: ='@newjson@'; - Dbms_output.put_line ('The JSON string is update:'|| JSON); $ INSERT into ddllog values ('@TABLE_NAME@','Update', json,sysdate); $ End If; -      - END [email protected][email protected];}'; theV_tmp:= REPLACE(V_tmp,'@[email protected]', tableName); -V_tmp:= REPLACE(V_tmp,'@[email protected]', Newjson);WuyiV_sql:= REPLACE(V_tmp,'@[email protected]', Oldjson); the Dbms_output.put_line (v_sql); -   EXECUTEIMMEDIATE V_sql; Wu EndLogging_messages;

Establishes a listener trigger for the specified table, with the following results:

Realized the expected function, there is still a lot of perfect space. But the level is lower, slowly to change it.

The stored procedure gives the specified table binding trigger SQL to spell JSON as the storage log

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.