熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Oracle >> 正文

在oracle中運行OS命令

2013-11-13 15:52:03  來源: Oracle 

  
  在Oracle i中往往會出現要在存儲過程中運行操作系統命令的情況一般來說利用Oracle Enterprise Manager設定作業時可以達到這個目的但是由於OEM在設定作業缺乏靈活性設定的作業的參數是固定的在實際應用當中往往需要在SQL語句當中運行需要隨時運行操作系統命令Oracle i沒有直接運行OS命令的語句我們可以利用DBMS_PIPE程序包實現這一要求
  DBMS_PIPE通過創建管道可以讓至少兩個進程進行通信Oracle的管道與操作系統的管道在概念上有相同的地方但是在實現機制不同
  下面介紹實現具體步驟:
   創建一個程序包姑且起名叫DAEMONSQL語句如下:
  /*創建daemon程序包*/
  CREATE OR REPLACE PACKAGE BODY daemon AS
  /*execute_system是實現運行os命令的函數*/
  FUNCTION execute_system(command VARCHAR
  timeout NUMBER DEFAULT )
  RETURN NUMBER IS
  status NUMBER;
  result VARCHAR();
  command_code NUMBER;
  pipe_name VARCHAR();
  BEGIN
  pipe_name := DBMS_PIPEUNIQUE_SESSION_NAME;
  DBMS_PIPEPACK_MESSAGE(SYSTEM);
  DBMS_PIPEPACK_MESSAGE(pipe_name);
  DBMS_PIPEPACK_MESSAGE(command);
  /*向daemon管道發送表示命令的字符*/
  status := DBMS_PIPESEND_MESSAGE(daemon timeout);
  IF status <> THEN
  RAISE_APPLICATION_ERROR(
  Execute_system: Error while sending Status = || status);
  END IF;
  status := DBMS_PIPERECEIVE_MESSAGE(pipe_name timeout);
  IF status <> THEN
  RAISE_APPLICATION_ERROR(
  Execute_system: Error while receiving
  Status = || status);
  END IF;
  /*獲取返回結果*/
  DBMS_PIPEUNPACK_MESSAGE(result);
  IF result <> done THEN
  RAISE_APPLICATION_ERROR(
  Execute_system: Done not received);
  END IF;
  DBMS_PIPEUNPACK_MESSAGE(command_code);
  DBMS_OUTPUTPUT_LINE(System command executed result = ||
  command_code);
  RETURN command_code;
  END execute_system;
  /*stop是讓daemon停止*/
  PROCEDURE stop(timeout NUMBER DEFAULT ) IS
  status NUMBER;
  BEGIN
  DBMS_PIPEPACK_MESSAGE(STOP);
  status := DBMS_PIPESEND_MESSAGE(daemon timeout);
  IF status <> THEN
  RAISE_APPLICATION_ERROR(
  stop: error while sending status = || status);
  END IF;
  END stop;
  END daemon;
  通過Sql*Plus運行以上語句將為當前用戶創建daemon程序包
   創建在OS上運行的守護進程監聽由上面的daemon程序包發來的要求執行os命令的語句以下Pro*C的代碼必須由pro*c先進行預編譯
  #include
  #include
  EXEC SQL INCLUDE SQLCA;
  EXEC SQL BEGIN DECLARE SECTION;
  char *uid = scott/tiger;/*在這個地方改為你自己訪問的用戶密碼服務名*/
  int status;
  VARCHAR command[];
  VARCHAR value[];
  VARCHAR return_name[];
  EXEC SQL END DECLARE SECTION;
  void
  connect_error()
  {
  char msg_buffer[];
  int msg_length;
  int buffer_size = ;
  EXEC SQL WHENEVER SQLERROR CONTINUE;
  sqlglm(msg_buffer &buffer_size &msg_length);
  printf(Daemon error while connecting:\n);
  printf(%*s\n msg_length msg_buffer);
  printf(Daemon quitting\n);
  exit();
  void
  sql_error()
  {
  char msg_buffer[];
  int msg_length;
  int buffer_size = ;
  EXEC SQL WHENEVER SQLERROR CONTINUE;
  sqlglm(msg_buffer &buffer_size &msg_length);
  printf(Daemon error while executing:\n);
  printf(%*s\n msg_length msg_buffer);
  printf(Daemon continuing\n);
  }
  main()
  {
  EXEC SQL WHENEVER SQLERROR DO connect_error();
  EXEC SQL CONNECT :uid;
  printf(Daemon connected\n);
  
  EXEC SQL WHENEVER SQLERROR DO sql_error();
  printf(Daemon waiting\n);
  while () {
  EXEC SQL EXECUTE
  BEGIN
  /*接收deamon發來的字符*/
  :status := DBMS_PIPERECEIVE_MESSAGE(daemon);
  IF :status = THEN
  /*取出字符*/
  DBMS_PIPEUNPACK_MESSAGE(:command);
  END IF;
  END;
  ENDEXEC;
  IF (status == )
  {
  commandarr[commandlen] = \;
  /*如果是stop該進程就退出*/
  IF (!strcmp((char *) commandarr STOP))
  {
  printf(Daemon exiting\n);
  break;
  }
  
  ELSE IF (!strcmp((char *) commandarr SYSTEM))
  {
  EXEC SQL EXECUTE
  BEGIN
  DBMS_PIPEUNPACK_MESSAGE(:return_name);
  DBMS_PIPEUNPACK_MESSAGE(:value);
  END;
  ENDEXEC;
  valuearr[valuelen] = \;
  printf(Will execute system command %s\n valuearr);
  /*運行os命令*/
  status = system(valuearr);
  EXEC SQL EXECUTE
  BEGIN
  DBMS_PIPEPACK_MESSAGE(done);
  DBMS_PIPEPACK_MESSAGE(:status);
  :status := DBMS_PIPESEND_MESSAGE(:return_name);
  END;
  ENDEXEC;
  
  IF (status)
  {
  printf
  (Daemon error while responding to system command);
  printf( status: %d\n status);
  }
  }
  ELSE
  {
  printf
  (Daemon error: invalid command %s received\n
  commandarr);
  }
  }
  ELSE
  {
  printf(Daemon error while waiting for signal);
  printf( status = %d\n status);
  }
  }
  EXEC SQL COMMIT WORK RELEASE;
  exit();
  }

From:http://tw.wingwit.com/Article/program/Oracle/201311/17426.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.