PLSQL block for run the job using dbms_scheduler f

2019-06-11 13:01发布

I want to create one PL/SQL block where try to run the job using dbms_scheduler package and I want to gather schema stats of all 30 schema. Eg:

begin
     dbms_scheduler_create_job(
     job_name => ....,
     job_type = > 'PL/SQL BLOCK',
     job_action => 'declare
      sch_lst dbms_stats.objecttab := dbms_stats.objecttab()
      begin
      sch_lst.extend(10);
      sch_lst(1).ownname := "ab";   --ab is the Schema name
      sch_lst(2).ownname := "cd";
      .........
      sch_lst(30).ownname := "xy";
      dbms_stats.gather_schema_stats( ......)
      end;
      /
      ',
     start_date => sysdate,
     ..........);
     end;
     /

2条回答
戒情不戒烟
2楼-- · 2019-06-11 13:29

Firstly, you may create such a procedure :

create or replace procedure pr_schema_stats is
  sch_lst owa.vc_arr;
begin
  sch_lst(1) := 'ab';
  sch_lst(2) := 'cd';  
  sch_lst(3) := 'ef';
  sch_lst(4) := 'gh';  
  sch_lst(5) := 'ij';
  sch_lst(6) := 'kl';  
  sch_lst(7) := 'mn';
  sch_lst(8) := 'op';  
  sch_lst(9) := 'rs';  
  sch_lst(10):= 'tu';  

 for i in 1..10
 loop    
   dbms_stats.gather_schema_stats(upper(sch_lst(i)),degree => 4, cascade => true );      
 end loop;
end; 

and then call from scheduler as :

declare
    v_job_name varchar2(70) := 'jb_gather_stats';
begin  
    dbms_scheduler.create_job(
        job_name => v_job_name,
        job_type => 'STORED_PROCEDURE',
        job_action => 'pr_schema_stats', 
        start_date => to_date('04-12-2018 19:00:00', 'dd-mm-yyyy hh24:mi:ss'),
        repeat_interval => 'FREQ=MONTHLY;INTERVAL=1;',
        auto_drop => false,
        comments => 'Produces statistics for Cost based SQL statements');

    dbms_scheduler.enable(v_job_name);    
end;

EDIT : You can replace your procedure's code with :

create or replace procedure pr_schema_stats is   
begin

 for c in (   
           select u.username,
                 row_number() over (order by u.username) as rn
            from dba_users u
           where u.account_status = 'OPEN' 
             and u.username not like 'SYS%' 
           )
 loop
  begin
    dbms_stats.gather_schema_stats(c.username,degree => 4, cascade => true );      
   exception when others then 
    dbms_output.put_line(sqlerrm);
  end;
 end loop;
end;

to include all of the ordinary schemas in the analyze task.

查看更多
【Aperson】
3楼-- · 2019-06-11 13:42

Before start_date => sysdate, remove / and also in the schema name instead of " (double quote) use '' (double single quote) because it is inside the declare statement which is already inside single quote.

begin
 dbms_scheduler_create_job(
 job_name => ....,
 job_type = > 'PL/SQL BLOCK',
 job_action => 'declare
  sch_lst dbms_stats.objecttab := dbms_stats.objecttab()
  begin
  sch_lst.extend(10);
  sch_lst(1).ownname := ''ab'';   --ab is the Schema name
  sch_lst(2).ownname := ''cd'';
  .........
  sch_lst(10).ownname := ''kl'';
  dbms_stats.gather_schema_stats( ......)
  end;
  ',
 start_date => sysdate,
 ..........);
 end;
 /

Then after compile this one. you can check the job by using execute dbms_schedule.run_job('<job_name>');

查看更多
登录 后发表回答