Bo's Oracle Station

查看: 2448|回复: 0

第68次:2014-11-20, 电子邮件通知和作业阵列

[复制链接]

1005

主题

1469

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12012
发表于 2014-11-21 15:39:17 | 显示全部楼层 |阅读模式
本帖最后由 botang 于 2014-11-27 16:04 编辑

2014-11-20-A.sql:
  1. create or replace procedure  hr.proc05317
  2. is
  3. begin
  4.    update employees set salary=salary+0.1 where employee_id=100;
  5.    commit;
  6. end;

  7. select  * from hr.employees where employee_id=100;

  8. begin
  9.    hr.proc05317;
  10. end;

  11. select  * from hr.employees where employee_id=100;


  12. begin
  13. DBMS_SCHEDULER.SET_SCHEDULER_ATTRIBUTE
  14. ('email_server','192.168.0.90:25');
  15. end;
  16. begin
  17. DBMS_SCHEDULER.SET_SCHEDULER_ATTRIBUTE
  18. ('email_sender','oracle@example.com');
  19. end;


  20. begin
  21.   DBMS_SCHEDULER.ADD_JOB_EMAIL_NOTIFICATION (
  22.   job_name  =>'HR.JOB1',
  23.   recipients    =>'oracle@example.com',
  24.   sender         =>'oracle@example.com',
  25.   events=>'job_started'
  26.   );
  27. end;

  28. select  * from dba_scheduler_notifications;


  29. create table hr.tbig as select  * from dba_source;

  30. select   s.RESOURCE_CONSUMER_GROUP  from v_$session s  where terminal='pts/18';
复制代码

2014-11-20-B.sql:
  1. select   owner,   j.job_name, j.job_style   from dba_scheduler_jobs  j ;

  2. create table hr.t05317_lw   ( a date ) ;

  3. create or replace procedure  hr.proc05317_lw
  4. is
  5. begin
  6.    insert into  hr.t05317_lw values(sysdate);
  7.    commit;
  8. end;

  9. select  * from hr.t05317_lw;

  10. begin
  11.     hr.proc05317_lw;
  12.     end;
  13.    
  14.     select  * from hr.t05317_lw;
  15.    
  16.    
  17.     -----EM programlw
  18.    
  19.    
  20.     begin
  21.    dbms_scheduler.create_job(
  22.    job_name => 'hr.joblw',
  23.    program_name=>'hr.programlw',
  24.    schedule_name=>'hr.schedule2',
  25.    job_style=>'LIGHTWEIGHT');
  26. end;

  27. select   owner,   j.job_name, j.job_style,j.state   from dba_scheduler_jobs  j ;

  28.   begin
  29.     dbms_scheduler.enable('HR.JOBLW');
  30. end;

  31.      select  * from hr.t05317_lw;
  32.      
  33.      ---------------------------------------------------------------------------------
  34.      truncate table hr.t05317_lw;
  35.      
  36.     select  * from hr.t05317_lw;
  37.      
  38.      DECLARE
  39. newjob sys.job;
  40. newjobarr sys.job_array;
  41. BEGIN
  42. -- Create an array of JOB object types
  43.   newjobarr := sys.job_array();
  44. -- Allocate sufficient space in the array
  45.   newjobarr.extend(100);
  46. -- Add definitions for jobs
  47. FOR i IN 1..100 LOOP
  48.    -- Create a JOB object type
  49.    newjob := sys.job(job_name => 'HR.JOBLW' || to_char(i),
  50.                      job_style => 'LIGHTWEIGHT',
  51.                      job_template => 'HR.PROGRAMLW',
  52.                    enabled => TRUE );
  53. -- Add job to the array
  54.    newjobarr(i) := newjob;
  55. END LOOP;
  56. -- Call CREATE_JOBS to create jobs in one transaction
  57. DBMS_SCHEDULER.CREATE_JOBS(newjobarr, 'TRANSACTIONAL');
  58. END;

  59.     select  * from hr.t05317_lw;
  60.    
  61.    
  62.     -------
复制代码

2014-11-20-C.sql:
  1. CREATE TABLE hr.scheduler_test (

  2.   id            NUMBER(10)    NOT NULL,

  3.   created_date  DATE          NOT NULL,

  4.   CONSTRAINT scheduler_test_pk PRIMARY KEY (id)

  5. );

  6. select  * from  hr.scheduler_test;

  7. CREATE SEQUENCE hr.scheduler_test_seq;

  8. CREATE OR REPLACE TYPE hr.t_event_queue_payload AS OBJECT (

  9.   event_name  VARCHAR2(30)

  10. );

  11. BEGIN

  12.   -- Create a queue table to hold the event queue.

  13.   DBMS_AQADM.create_queue_table(

  14.     queue_table        => 'hr.event_queue_tab',

  15.     queue_payload_type => 'hr.t_event_queue_payload',

  16.     multiple_consumers => TRUE,

  17.     comment            => 'Queue Table For Event Messages');



  18.   -- Create the event queue.

  19.   DBMS_AQADM.create_queue (

  20.     queue_name  => 'hr.event_queue',

  21.     queue_table => 'hr.event_queue_tab');



  22.   -- Start the event queue.

  23.   DBMS_AQADM.start_queue (queue_name => 'hr.event_queue');

  24. END;



  25. BEGIN

  26.    DBMS_SCHEDULER.create_job (

  27.       job_name        => 'hr.event_based_job',

  28.       job_type        => 'PLSQL_BLOCK',

  29.       job_action      => 'BEGIN

  30.                             INSERT INTO hr.scheduler_test (id, created_date)

  31.                             VALUES (scheduler_test_seq.NEXTVAL, SYSDATE);

  32.                             COMMIT;

  33.                           END;',

  34.       start_date      => SYSTIMESTAMP,

  35.       event_condition => 'tab.user_data.event_name = ''give_me_a_prod''',

  36.       queue_spec      => 'hr.event_queue',

  37.       enabled         => TRUE);

  38. END;

  39. select  * from  hr.scheduler_test;


  40. DECLARE

  41.   l_enqueue_options     DBMS_AQ.enqueue_options_t;

  42.   l_message_properties  DBMS_AQ.message_properties_t;

  43.   l_message_handle      RAW(16);

  44.   l_queue_msg           hr.t_event_queue_payload;

  45. BEGIN

  46.   l_queue_msg := hr.t_event_queue_payload('give_me_a_prod');



  47.   DBMS_AQ.enqueue(queue_name          => 'hr.event_queue',

  48.                   enqueue_options     => l_enqueue_options,

  49.                   message_properties  => l_message_properties,

  50.                   payload             => l_queue_msg,

  51.                   msgid               => l_message_handle);

  52.   COMMIT;

  53. END;

  54. select  * from  hr.scheduler_test;

  55. --------------------------------------------------------------------------------------------------------------------------------------------


  56. truncate table hr.scheduler_test;

  57. select hr.scheduler_test_seq.currval from dual;

  58. -----
  59. ------
  60. create or replace procedure proc_test( p_1  number , p_2  out   number)
  61. is
  62. begin
  63.   update employees set salary=10000 where employee_id=p_1;
  64.   select  salary into p_2 from  employees  where employee_id=p_1;
  65. end;

  66. select  * from user_errors;


  67. declare
  68.   v_salary  number(8,2);
  69. begin
  70.   proc_test(100,v_salary);
  71.   dbms_output.put_line(v_salary);
  72. end;
  73. ----
  74. create or replace procedure  proc_test_io( p_1  in out number )  
  75. is
  76. begin
  77.   p_1 := p_1*2;
  78.   
  79. end;

  80. select  * from user_errors;

  81. ------






  82. ---
  83. declare
  84.   v_1 number;
  85. begin
  86.    v_1 := 1;
  87.    proc_test_io( v_1);
  88.    dbms_output.put_line(v_1);
  89. end;

  90. -----
  91. -----------------------------------------------------------------------------------
  92. create or replace procedure  hr.procnew( p_1     hr.t_event_queue_payload    )
  93. is

  94. BEGIN
  95.      INSERT INTO hr.scheduler_test (id, created_date, event_name)
  96.          VALUES (hr.scheduler_test_seq.NEXTVAL, SYSDATE, p_1.event_name  );
  97.          COMMIT;
  98. END;



  99. select  * from user_errors;


  100. begin
  101.   dbms_scheduler.create_program (
  102.    program_name => 'hr.programnew',
  103.     program_action=> 'hr.procnew',
  104.     program_type => 'STORED_PROCEDURE',
  105.     number_of_arguments => 1,
  106.     enabled => false) ;
  107. end;


  108. select  * from dba_scheduler_programs  p where p.owner='HR';


  109. select  * from dba_scheduler_program_args   pa where pa.owner='HR' ;


  110. begin
  111.    dbms_scheduler.define_metadata_argument (
  112.     program_name => 'hr.programnew',
  113.     argument_position => 1 ,
  114.    metadata_attribute => 'EVENT_MESSAGE') ;
  115.    
  116. end;


  117. select  * from dba_scheduler_program_args   pa where pa.owner='HR' ;


  118. begin
  119. dbms_scheduler.enable ('hr.programnew');
  120. end;


  121. alter table    hr.scheduler_test add ( event_name  varchar2(40)) ;



  122. select  * from hr.scheduler_test;


  123. BEGIN
  124. sys.dbms_scheduler.create_job(
  125. job_name => '"HR"."JOBNEW"',
  126. program_name => '"HR"."PROGRAMNEW"',
  127. event_condition => 'tab.user_data.event_name = ''give_me_a_prod''',
  128. queue_spec => '"HR"."EVENT_QUEUE"',
  129. start_date => systimestamp at time zone 'Asia/Shanghai',
  130. job_class => '"DEFAULT_JOB_CLASS"',
  131. comments => 'jobnew',
  132. auto_drop => FALSE,
  133. enabled => TRUE);


  134. select  * from hr.scheduler_test;


  135. DECLARE

  136.   l_enqueue_options     DBMS_AQ.enqueue_options_t;

  137.   l_message_properties  DBMS_AQ.message_properties_t;

  138.   l_message_handle      RAW(16);

  139.   l_queue_msg           hr.t_event_queue_payload;

  140. BEGIN

  141.   l_queue_msg := hr.t_event_queue_payload('give_me_a_prod');



  142.   DBMS_AQ.enqueue(queue_name          => 'hr.event_queue',

  143.                   enqueue_options     => l_enqueue_options,

  144.                   message_properties  => l_message_properties,

  145.                   payload             => l_queue_msg,

  146.                   msgid               => l_message_handle);

  147.   COMMIT;

  148. END;

  149. select  * from hr.scheduler_test;
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Bo's Oracle Station   

GMT+8, 2024-12-5 02:56 , Processed in 0.039427 second(s), 24 queries .

快速回复 返回顶部 返回列表