Using the System Package dbms_alert monitoring table

Source: Internet
Author: User
Tags sqlplus

Source: http://blog.csdn.net/edcvf3/article/details/8285328

Dbms_alert supports asynchronous capture of database events.

Now test its capture table for various changes, the test table is EMP.

Because it is asynchronous, we need to open 2 Sqlplus windows (two sessions)

The first session writes the following stored procedures:

(Note that to give Scott user permission first,

Grant execute on Dbms_alert to Scott)

[SQL]View PlainCopyprint?
  1. Declare
  2. V_n VARCHAR2 (30);
  3. V_m varchar2 (100);
  4. V_i Integer;
  5. Begin
  6. Dbms_alert.removeall;
  7. Dbms_alert.register (' Emp_table_alert ');
  8. Dbms_alert.waitany (v_n,v_m,v_i);
  9. If l_i=0 Then
  10. Dbms_output.put_line (' [name:] ' | |  L_n);
  11. Dbms_output.put_line (' [msg:] ' | |  L_M);
  12. End If;
  13. Dbms_alert.removeall;
  14. End
  15. /

After executing this stored procedure, this session will be in the waiting state ...

We re-open a sqlplus and set up the trigger as follows:

[SQL]View PlainCopyprint?
    1. Create or replace trigger emtring
    2. After insert or update or delete on emp
    3. Begin
    4. Dbms_alert.signal (' Emp_table_alert ', To_char (sysdate,' YYYYMMDD ') | |  ': Table emp is changed ');
    5. End


Where dbms_alert.signal is used to send alerts, and under this session, make some changes, such as insert

[SQL]View PlainCopyprint?
    1. INSERT INTO emp (empno,ename) values (1234,' w001 ');
    2. Commit

Note that you must commit, because Dbms_alert is transaction-based.

Once commit, the waiting state of the session in the first sqlplus is ended, and the output is as follows:

[SQL]View PlainCopyprint?
    1. 10:18:18 [Email protected]>/
    2. [Name:]emp_table_alert
    3. [msg:]20121212:table emp is changed


This package is typically used in development, capturing various alerts in the DB that occur in application for processing.

Using the System Package dbms_alert monitoring table

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.