如何在Oracle 11g中使用SQLPLUS在包主体中调用过程

时间:2018-12-14 19:43:31

标签: oracle stored-procedures plsql oracle11g sqlplus

我正在使用Oracle 11g。以下过程将txt文件转换为xls文件。我想通过SQLPLUS调用此过程。包主体的名称为"VMDB_OAS_doc_utils" 过程名称是 "Convert_Multi_Sheet" 这些参数是 source_path, target_path, and opts。 txt文件的目录位置为/isweb/www/ss/issapt/vmdb/adhoc_reports/vwar_444026.txt. 如何编写调用此过程的脚本,以便将该txt文件转换为xls文件? 这是以下过程的代码:

---------------------------
--- This procedure allows a tab-delimited file to be converted 
--  into a multi-sheet MS-Excel file. The dbsm_scheduler is used to schedule a server side 
--  job to do the conversion. Scheduler tables/views are queried for status and this routine
--  busy waits for the process to complete. Afterwards error return status is returned in the 
--  err_msg parameter. After more investigation, it was determined that java would be a superior
--  approach, primarily because it provides for finer granularity in controlling execution priviliges.
--  However, it was too late to make the 951 release.
---
--- @@param source_path - a string containing the full file path of the source.
--- @@param target_path - a string containing the full target file path. 
---
--- @@param opts - a string containing the options to the sv2xls.pl 
---    command as entered as if at a terminal (including the dash).
---
--- @@return err_msg - a string containing error message.  Empty otherwise.
---------------------------
   procedure Convert_Multi_Sheet (
      source_path in varchar2,
      target_path in out varchar2,
      opts in varchar2 := NULL,
      err_msg      out varchar2
   ) is
      result boolean;  
      -- The command to run
      command varchar2(32767):= vmdb_web.get_var_val('VMDB_RUNLIB');
      -- The name of the job, generated 
      jobname varchar2(40);
      number_of_arguments PLS_INTEGER :=2;
      argument_position PLS_INTEGER:= 1;
      l_opts_array vmdb_oas_utils.ISAC_ident_arr;
      -- Get the completion status of the passed jobname. 
      -- Will time out after six hours. 
      -- 
      -- @@param jobname the job to get completions status from.
      -- @@returns any error message caused by running the job or null if no error was detected.
      --
      function get_completion_status(jobname varchar2) return varchar2 is
       result varchar2(32767):='RUNNING'; -- Status
       stop_date date:= sysdate + 6/24; -- Stop the date after six hours if it hasn't quit yet
      begin
        while result = 'RUNNING' and sysdate < stop_date loop
      begin
          SELECT upper(STATE) into result FROM all_scheduler_jobs where job_name=jobname ;
          -- The job may return successfully if one of these occurs.
          if result in ('SCHEDULED', 'RUNNING', 'RETRY SCHEDULED') then
          result:= 'RUNNING';
          end if;
         exception
          when no_data_found then
            -- Job has completed. Normal execution returns null (no error).
        result:= null;
     end;
    end loop;
    if nvl(result, '<NULL>') = '<NULL>' and sysdate >= stop_date then
      result:= 'Error: process has timed out!';
    else
      begin
        SELECT upper(STATUS) into result FROM ALL_SCHEDULER_JOB_RUN_DETAILS where job_name=jobname;
        -- Successful statuses
        if result = 'SUCCEEDED'  or result = 'COMPLETED'  then
          result:= null;
        else 
          select status||chr(10)||'ERROR#:'||ERROR#||chr(10)||'INFO:'|| ADDITIONAL_INFO into result from ALL_SCHEDULER_JOB_RUN_DETAILS
            where job_name=jobname;
        end if;
       exception
        when no_data_found then
          result:= 'No data found.';
       end;
    end if;
    return result;
  end get_completion_status;
   begin
  if regexp_like(target_path, '*.xlsx', 'i') then
     command:= command||'/sv2xlsx.pl';
     jobname := DBMS_SCHEDULER.GENERATE_JOB_NAME(prefix=>'sv2xlsx$_');
  else
     command:= command||'/sv2xls.pl';
     jobname := DBMS_SCHEDULER.GENERATE_JOB_NAME(prefix=>'sv2xls$_');
  end if;
  -- Debug - only prints out when serveroutput is set on
  dbms_output.put_line('jobname is '|| jobname);
  dbms_output.put_line('command is '|| command);
  if nvl(opts, '<NULL>') != '<NULL>' then
    -- options?
    l_opts_array:= vmdb_oas_utils.varchar_2_ident_array (opts, '-');
    -- ASR 115302 - Scheduler is choking on multiple dash (-) args in
    --  single argument position - so split them into seperate args.
    --  Here we get the count.
    for i in l_opts_array.FIRST..l_opts_array.LAST loop
       if nvl(l_opts_array(i), '<NULL>') != '<NULL>' then
         number_of_arguments:=number_of_arguments+1;
       end if;
    end loop;
  end if;
  -- Create that job
  DBMS_SCHEDULER.CREATE_JOB(
    job_name=>jobname,
    job_type=> 'executable',
    job_action=> command,
    enabled=> FALSE,
    number_of_arguments=>number_of_arguments
  );
  -- Set the options argument, if opts was passed
  if nvl(opts, '<NULL>') != '<NULL>' then
    -- ASR 115302 - Scheduler is choking on multiple dash (-) args in
    --  single argument position - so split them into seperate args.
    for i in l_opts_array.FIRST..l_opts_array.LAST loop
       if nvl(l_opts_array(i), '<NULL>') != '<NULL>' then
          DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE (
            job_name          => jobname,
            argument_position => argument_position,
            argument_value    => '-'||ltrim(rtrim(l_opts_array(i)))
          );
          argument_position:= argument_position+1;
       end if;
    end loop;
  end if;
  -- Set the source path
  DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE (
      job_name          => jobname,
      argument_position => argument_position,
      argument_value    => source_path
    );
  argument_position:= argument_position+1;
  -- Set the target path
  DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE (
      job_name          => jobname,
      argument_position => argument_position,
      argument_value    => target_path
    );
  -- This caused the job to run
  DBMS_SCHEDULER.ENABLE(jobname);
  dbms_output.put_line('called job ');
  -- This will busy wait until job has completed
  err_msg:= get_completion_status(jobname);
end Convert_Multi_Sheet;

1 个答案:

答案 0 :(得分:1)

您可以使用EXEC命令使用绑定变量为OUT参数调用SQL * Plus中的过程。

VARIABLE v_err_msg varchar2
EXEC VMDB_OAS_doc_utils.Convert_Multi_Sheet (
      source_path =>  '/source/path/file.txt' 
      ,target_path =>  'target/path/filename.xls' --set it or let the 
                                                 --procedure set a path for you
      ,opts        =>  NULL
      ,err_msg     => :v_err_msg
   )
PRINT v_err_msg --prints the value returned from procedure.

如果未提供sv2xls.pl,则不清楚target_path会做什么。它 显然定义为IN OUT,这意味着您也可以在调用的同时传递已分配的变量,并从过程中接收更新后的值。

现在,您可以使用SQL * Plus以多种方式执行这些命令,具体取决于您的操作系统(Windows / Linux)。对于Unix / Linux,请参见this;对于Windows,请参见this