|
本帖最后由 botang 于 2015-6-3 08:43 编辑
- select from dba_scheduler_windows;
- select username, resource_consumer_group from v$session where terminal='pts/6';
- select username , u.initial_rsrc_consumer_group from dba_users u where username in ('SYS','SYSTEM','HR');
- select * from dba_rsrc_plans;
- BEGIN
- dbms_resource_manager.clear_pending_area();
- dbms_resource_manager.create_pending_area();
- dbms_resource_manager.create_plan( plan => 'PLAN2', comment => 'PLAN2',mgmt_mth => 'RATIO' );
- dbms_resource_manager.create_plan_directive(
- plan => 'PLAN2',
- group_or_subplan => 'SYS_GROUP',
- comment => 'SYS_GROUP',
- mgmt_p1 => 5
- );
- dbms_resource_manager.create_plan_directive(
- plan =>'PLAN2' ,
- group_or_subplan => 'GROUP1',
- comment => 'GROUP1',
- mgmt_p1 => 3
- );
- dbms_resource_manager.create_plan_directive(
- plan => 'PLAN2',
- group_or_subplan => 'GROUP2',
- comment => 'GROUP2',
- mgmt_p1 => 2
- );
- dbms_resource_manager.create_plan_directive(
- plan => 'PLAN2',
- group_or_subplan => 'OTHER_GROUPS',
- comment => 'OTHER_GROUPS',
- mgmt_p1 => 1
- );
- dbms_resource_manager.submit_pending_area();
- END;
- select * from dba_rsrc_plan_directives rpd where rpd.plan='PLAN1';
- select degree from dba_indexes where index_name='IBIG' and owner='HR';
- select username, status ,resource_consumer_group from v$session where username='HR' ;
复制代码
复杂scheduler
- BEGIN
- /* Complex scheduling example 1: Public Holidays */
- DBMS_SCHEDULER.CREATE_SCHEDULE
- (schedule_name => 'SCHD_2008_PUBLIC_HOLIDAYS'
- ,start_date => to_timestamp_tz('01/01/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
- ,end_date => to_timestamp_tz('31/12/2008 23:59:59','dd/mm/yyyy hh24:mi:ss')
- ,repeat_interval =>
- 'freq=DAILY;
- bydate=20080101,20080128,20080303,20080321,20080324,20080425,
- 20080929,20081225,20081226'
- ,comments => 'Public Holidays 2008'
- );
- END;
- /
- The following example creates a schedule consisting of all 2008 working days:
- BEGIN
- /* Complex scheduling example 2: Working Days */
- DBMS_SCHEDULER.CREATE_SCHEDULE
- (schedule_name => 'SCHD_2008_WORKING_DAYS'
- ,start_date => to_timestamp_tz('01/01/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
- ,end_date => to_timestamp_tz('31/12/2008 23:59:59','dd/mm/yyyy hh24:mi:ss')
- ,repeat_interval =>
- 'freq=DAILY;
- byday=MON,TUE,WED,THU,FRI;
- exclude=SCHD_2008_PUBLIC_HOLIDAYS'
- ,comments => 'Run on every working day'
- );
- END;
- /
- The following example creates a schedule consisting of the first working day of every month in 2008:
- BEGIN
- /* Complex scheduling example 3: 1st Working Day in a Month */
- DBMS_SCHEDULER.CREATE_SCHEDULE
- (schedule_name => 'SCHD_2008_1ST_WK_DAY_EACH_MTH'
- ,start_date => to_timestamp_tz('01/01/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
- ,end_date => to_timestamp_tz('31/12/2008 23:59:59','dd/mm/yyyy hh24:mi:ss')
- ,repeat_interval =>
- 'freq=MONTHLY;
- byday=MON,TUE,WED,THU,FRI;
- exclude=SCHD_2008_PUBLIC_HOLIDAYS;
- bysetpos=1'
- ,comments => 'Run on 1st working day every month'
- );
- END;
- /
- -------------------------------------------------------------
- BEGIN
- /* Complex scheduling example 4: 1st and 3rd Monday each month */
- DBMS_SCHEDULER.CREATE_SCHEDULE
- (schedule_name => 'SCHD_1ST_3RD_MON_EACH_MTH'
- ,start_date => to_timestamp_tz('03/12/2007 05:00:00','dd/mm/yyyy hh24:mi:ss')
- ,repeat_interval =>
- 'freq=MONTHLY;
- byday=1MON,3MON'
- ,comments => 'Run on every 1st and 3rd Monday each month'
- );
- END;
- /
- The following example creates a schedule consisting of the last day of each quarter:
- BEGIN
- /* Complex scheduling example 5: Quarter */
- DBMS_SCHEDULER.CREATE_SCHEDULE
- (schedule_name => 'SCHD_LAST_DAY_EACH_QTR'
- ,start_date => to_timestamp_tz('31/12/2008 05:00:00','dd/mm/yyyy hh24:mi:ss')
- ,repeat_interval =>
- 'freq=YEARLY;
- bymonth=MAR,JUN,SEP,DEC;
- bymonthday=-1'
- ,comments => 'Run on every last day each quarter'
- );
- END;
- /
- -------------------------------------------------------------
- CREATE OR REPLACE PROCEDURE show_schedule
- (schedule_name in varchar2
- ,start_date in timestamp
- ,repeat_interval in varchar2
- ,iteration in number)
- AS
- v_start_date timestamp;
- v_after_date timestamp;
- v_next_date timestamp;
- BEGIN
- dbms_output.put_line (schedule_name);
- v_start_date := start_date;
- v_after_date := v_start_date-(1/86400);
- for i in 1..iteration loop
- dbms_scheduler.evaluate_calendar_string
- (repeat_interval
- ,v_start_date
- ,v_after_date
- ,v_next_date
- );
- dbms_output.put_line (v_next_date);
- v_after_date:= v_next_date;
- end loop;
- dbms_output.put_line ('============================');
- END;
- /
- -- --------------------------------------------------------
- set serveroutput on
- exec dbms_output.enable (1000000);
- begin
- show_schedule
- (schedule_name => 'SCHD_2008_PUBLIC_HOLIDAYS'
- ,start_date => to_timestamp_tz('01/01/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
- ,repeat_interval =>
- 'freq=DAILY;
- bydate=20080101,20080128,20080303,20080321,20080324,20080425,
- 20080929,20081225,20081226'
- ,iteration => 9);
- show_schedule
- (schedule_name => 'SCHD_2008_WORKING_DAYS'
- ,start_date => to_timestamp_tz('01/01/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
- ,repeat_interval =>
- 'freq=DAILY;
- byday=MON,TUE,WED,THU,FRI;
- exclude=SCHD_2008_PUBLIC_HOLIDAYS'
- ,iteration => 21);
- show_schedule
- (schedule_name => 'SCHD_2008_1ST_WK_DAY_EACH_MTH'
- ,start_date => to_timestamp_tz('01/01/2008 00:00:00','dd/mm/yyyy hh24:mi:ss')
- ,repeat_interval =>
- 'freq=MONTHLY;
- byday=MON,TUE,WED,THU,FRI;
- exclude=SCHD_2008_PUBLIC_HOLIDAYS;
- bysetpos=1'
- ,iteration => 12);
- show_schedule
- (schedule_name => 'SCHD_1ST_3RD_MON_EACH_MTH'
- ,start_date => to_timestamp_tz('03/12/2007 05:00:00','dd/mm/yyyy hh24:mi:ss')
- ,repeat_interval =>
- 'freq=MONTHLY;
- byday=1MON,3MON'
- ,iteration => 26);
- show_schedule
- (schedule_name => 'SCHD_LAST_DAY_EACH_QTR'
- ,start_date => to_timestamp_tz('31/12/2008 05:00:00','dd/mm/yyyy hh24:mi:ss')
- ,repeat_interval =>
- 'freq=YEARLY;
- bymonth=MAR,JUN,SEP,DEC;
- bymonthday=-1'
- ,iteration => 5);
- end;
- /
- set serverout off
- -- ---------------------
复制代码
event_based_jobs.sql:
- CONN sys/oracle AS SYSDBA
- DROP USER test CASCADE;
- CREATE USER test IDENTIFIED BY test QUOTA UNLIMITED ON USERS;
- GRANT CONNECT TO test;
- GRANT CREATE TABLE TO test;
- GRANT CREATE SEQUENCE TO test;
- GRANT CREATE TYPE TO test;
- GRANT AQ_ADMINISTRATOR_ROLE TO test;
- GRANT CREATE JOB TO test;
- CONN test/test
- DROP TABLE scheduler_test;
- DROP SEQUENCE scheduler_test_seq;
- CREATE TABLE scheduler_test (
- id NUMBER(10) NOT NULL,
- created_date DATE NOT NULL,
- CONSTRAINT scheduler_test_pk PRIMARY KEY (id)
- );
- CREATE SEQUENCE scheduler_test_seq;
- CREATE OR REPLACE TYPE t_event_queue_payload AS OBJECT (
- event_name VARCHAR2(30)
- );
- /
- BEGIN
- -- Create a queue table to hold the event queue.
- DBMS_AQADM.create_queue_table(
- queue_table => 'event_queue_tab',
- queue_payload_type => 't_event_queue_payload',
- multiple_consumers => TRUE,
- comment => 'Queue Table For Event Messages');
- -- Create the event queue.
- DBMS_AQADM.create_queue (
- queue_name => 'event_queue',
- queue_table => 'event_queue_tab');
- -- Start the event queue.
- DBMS_AQADM.start_queue (queue_name => 'event_queue');
- END;
- /
- BEGIN
- DBMS_SCHEDULER.create_job (
- job_name => 'event_based_job',
- job_type => 'PLSQL_BLOCK',
- job_action => 'BEGIN
- INSERT INTO scheduler_test (id, created_date)
- VALUES (scheduler_test_seq.NEXTVAL, SYSDATE);
- COMMIT;
- END;',
- start_date => SYSTIMESTAMP,
- event_condition => 'tab.user_data.event_name = ''give_me_a_prod''',
- queue_spec => 'event_queue',
- enabled => TRUE);
- END;
- /
- SELECT * FROM scheduler_test;
- DECLARE
- l_enqueue_options DBMS_AQ.enqueue_options_t;
- l_message_properties DBMS_AQ.message_properties_t;
- l_message_handle RAW(16);
- l_queue_msg t_event_queue_payload;
- BEGIN
- l_queue_msg := t_event_queue_payload('give_me_a_prod');
- DBMS_AQ.enqueue(queue_name => 'event_queue',
- enqueue_options => l_enqueue_options,
- message_properties => l_message_properties,
- payload => l_queue_msg,
- msgid => l_message_handle);
- COMMIT;
- END;
- /
- /*
- SELECT * FROM scheduler_test;
- -- Remove the job.
- EXEC DBMS_SCHEDULER.drop_job('event_based_job');
-
- -- Stop the event queue.
- EXEC DBMS_AQADM.stop_queue (queue_name => 'event_queue');
- -- Drop the event queue.
- EXEC DBMS_AQADM.drop_queue (queue_name => 'event_queue');
- -- Remove the queue table.
- EXEC DBMS_AQADM.drop_queue_table(queue_table => 'event_queue_tab');
- DROP TYPE t_event_queue_payload;
- DROP TABLE scheduler_test;
- DROP SEQUENCE scheduler_test_seq;
- PURGE RECYCLEBIN;
- */
复制代码
04314chain.sql
- CREATE TABLE scheduler_test (
- id NUMBER(10) NOT NULL,
- description VARCHAR2(20) NOT NULL,
- created_date DATE NOT NULL,
- CONSTRAINT scheduler_test_pk PRIMARY KEY (id)
- );
- CREATE SEQUENCE scheduler_test_seq;
- BEGIN
- DBMS_SCHEDULER.create_program (
- program_name => 'test_program_1',
- program_type => 'PLSQL_BLOCK',
- program_action => 'BEGIN
- INSERT INTO scheduler_test (id, description, created_date)
- VALUES (scheduler_test_seq.NEXTVAL, ''test_program_1'', SYSDATE);
- COMMIT;
- END;',
- enabled => TRUE,
- comments => 'Program for first link in the chain.');
- DBMS_SCHEDULER.create_program (
- program_name => 'test_program_2',
- program_type => 'PLSQL_BLOCK',
- program_action => 'BEGIN
- INSERT INTO scheduler_test (id, description, created_date)
- VALUES (scheduler_test_seq.NEXTVAL, ''test_program_2'', SYSDATE);
- COMMIT;
- END;',
- enabled => TRUE,
- comments => 'Program for second link in the chain.');
- DBMS_SCHEDULER.create_program (
- program_name => 'test_program_3',
- program_type => 'PLSQL_BLOCK',
- program_action => 'BEGIN
- INSERT INTO scheduler_test (id, description, created_date)
- VALUES (scheduler_test_seq.NEXTVAL, ''test_program_3'', SYSDATE);
- COMMIT;
- END;',
- enabled => TRUE,
- comments => 'Program for last link in the chain.');
- END;
- CREATE OR REPLACE TYPE t_event_queue_payload AS OBJECT (
- event_name VARCHAR2(30)
- );
- BEGIN
- -- Create a queue table to hold the event queue.
- DBMS_AQADM.create_queue_table(
- queue_table => 'event_queue_tab',
- queue_payload_type => 't_event_queue_payload',
- multiple_consumers => TRUE,
- comment => 'Queue Table For Event Messages');
- -- Create the event queue.
- DBMS_AQADM.create_queue (
- queue_name => 'event_queue',
- queue_table => 'event_queue_tab');
- -- Start the event queue.
- DBMS_AQADM.start_queue (queue_name => 'event_queue');
- END;
- BEGIN
- sys.dbms_scheduler.create_event_schedule(
- event_condition => 'tab.user_data.event_name = ''give_me_a_orange''',
- queue_spec => 'TEST.EVENT_QUEUE',
- start_date => systimestamp at time zone '+8:00',
- comments => 'schedule_event',
- schedule_name => '"TEST"."SCHEDULE_EVENT"');
- END;
- BEGIN
- DBMS_SCHEDULER.create_chain (
- chain_name => 'test_chain_1',
- rule_set_name => NULL,
- evaluation_interval => NULL,
- comments => 'A test chain.');
- END;
- SELECT
- chain_name,
- rule_set_owner,
- rule_set_name,
- number_of_rules,
- number_of_steps,
- enabled,
- comments
- FROM user_scheduler_chains;
- BEGIN
-
- DBMS_SCHEDULER.define_chain_event_step (
- chain_name => 'test_chain_1',
- step_name => 'chain_step_1',
- event_schedule_name => 'TEST.SCHEDULE_EVENT');
- DBMS_SCHEDULER.define_chain_step (
- chain_name => 'test_chain_1',
- step_name => 'chain_step_2',
- program_name => 'test_program_1');
- DBMS_SCHEDULER.define_chain_step (
- chain_name => 'test_chain_1',
- step_name => 'chain_step_3',
- program_name => 'test_program_2');
- DBMS_SCHEDULER.define_chain_step (
- chain_name => 'test_chain_1',
- step_name => 'chain_step_4',
- program_name => 'test_program_3');
- END;
- BEGIN
-
- DBMS_SCHEDULER.define_chain_rule (
-
- chain_name => 'test_chain_1',
- condition => '1=1',
- action => 'START chain_step_1',
- rule_name => 'chain_rule_1',
- comments => 'First link in the chain.');
- DBMS_SCHEDULER.define_chain_rule (
-
- chain_name => 'test_chain_1',
- condition => 'chain_step_1 completed',
- action => 'START chain_step_2',
- rule_name => 'chain_rule_2',
- comments => 'Second link in the chain.');
- DBMS_SCHEDULER.define_chain_rule (
- chain_name => 'test_chain_1',
- condition => 'chain_step_2 completed',
- action => 'START chain_step_3',
- rule_name => 'chain_rule_3',
- comments => 'Third link in the chain.');
- DBMS_SCHEDULER.define_chain_rule (
- chain_name => 'test_chain_1',
- condition => 'chain_step_3 completed',
- action => 'START chain_step_4',
- rule_name => 'chain_rule_4',
- comments => 'Fourth link in the chain.');
- DBMS_SCHEDULER.define_chain_rule (
- chain_name => 'test_chain_1',
- condition => 'chain_step_4 completed',
- action => 'END',
- rule_name => 'chain_rule_5',
- comments => 'End of the chain.');
- END;
- BEGIN
- DBMS_SCHEDULER.ENABLE ('test_chain_1');
- END;
- SELECT *
- FROM user_scheduler_running_chains;
- select * from scheduler_test;
- DECLARE
- l_enqueue_options DBMS_AQ.enqueue_options_t;
- l_message_properties DBMS_AQ.message_properties_t;
- l_message_handle RAW(16);
- l_queue_msg t_event_queue_payload;
- BEGIN
- l_queue_msg := t_event_queue_payload('give_me_a_orange');
- DBMS_AQ.enqueue(queue_name => 'event_queue',
- enqueue_options => l_enqueue_options,
- message_properties => l_message_properties,
- payload => l_queue_msg,
- msgid => l_message_handle);
- COMMIT;
- END;
复制代码
|
|