将Oracle过程(具有特定函数调用)移植到Postgres PL / PGSQL

时间:2010-02-17 03:48:07

标签: database oracle postgresql plpgsql

(这与我之前提到的问题类似:Porting Oracle Procedure to PostgreSQL

我需要移植:

/*
||  The following private procedure will execute a dynamic pl/sql
||  statement passed to it.
*/
   CREATE OR REPLACE FUNCTION DB_SHELL_UTIL_PKG.EXECUTE_STMT (stmt VARCHAR) IS
    v_num_rows      integer;
    v_cursor_table  integer;
  BEGIN
    v_cursor_table := dbms_sql.open_cursor;
    dbms_sql.parse (v_cursor_table, stmt, dbms_sql.v7);
    v_num_rows := dbms_sql.execute (v_cursor_table);
    dbms_sql.close_cursor(v_cursor_table);
  END execute_stmt;

/*
||  The following private procedure will write out to a system
||  file the statement passed to it.
*/
   CREATE OR REPLACE FUNCTION DB_SHELL_UTIL_PKG.write_log_info (p_path IN VARCHAR2,
                             p_file_name IN VARCHAR2,
                             stmt IN VARCHAR2 ) IS
      log_file UTL_FILE.FILE_TYPE;
   BEGIN
      log_file := UTL_FILE.FOPEN (p_path, p_file_name, 'A');
      UTL_FILE.PUT_LINE (log_file,stmt);
      UTL_FILE.FCLOSE(log_file);
   EXCEPTION
      WHEN OTHERS THEN
      UTL_FILE.FCLOSE(log_file);
      RAISE;
   END write_log_info;

/*
||  The following procedure will drop the user passed to it
|| and then record its action by writing out to a system file
*/
  PROCEDURE DROP_DB_PRO (  p_db_name IN VARCHAR2,
                  p_path IN VARCHAR2,
                  p_file_name IN VARCHAR2,
                  p_status OUT VARCHAR2  ) IS
    v_stmt  VARCHAR2(1500);
  BEGIN
    v_stmt :=  'DROP USER '||p_db_name||' CASCADE';
    execute_stmt (v_stmt);
    p_status := 'USER '||p_db_name|| ' HAS BEEN DROPPED';
    v_stmt :=  'THE USER '||p_db_name||' HAS BEEN DROPPED';
    write_log_info(p_path, p_file_name, v_stmt);
  EXCEPTION
    WHEN OTHERS THEN
    v_stmt := 'EXCEPTION raised in DROP_DB_PRO';
    write_log_info (p_path, p_file_name, v_stmt);
    RAISE;
  END DROP_DB_PRO;

从Oracle到PG / PLSQL ......

我已经走到了这一步:

  CREATE OR REPLACE FUNCTION DB_SHELL_UTIL_PKG.EXECUTE_STMT (stmt VARCHAR)
  RETURNS void as '
  DECLARE
    v_num_rows      integer;
    v_cursor_table  integer;
  BEGIN
    v_cursor_table := dbms_sql.open_cursor;
    dbms_sql.parse (v_cursor_table, stmt, dbms_sql.v7);
    v_num_rows := dbms_sql.execute (v_cursor_table);
    dbms_sql.close_cursor(v_cursor_table);
  END execute_stmt;
' LANGUAGE plpgsql;

窒息:

ERROR:  syntax error at or near "dbms_sql"
LINE 1: dbms_sql.parse ( $1 ,  $2 , dbms_sql.v7)

CREATE OR REPLACE FUNCTION DB_SHELL_UTIL_PKG.write_log_info (p_path VARCHAR,
                             p_file_name VARCHAR,
                             stmt VARCHAR ) 
RETURNS void as '
      log_file UTL_FILE.FILE_TYPE;
   BEGIN
      log_file := UTL_FILE.FOPEN (p_path, p_file_name, ''A'');
      UTL_FILE.PUT_LINE (log_file,stmt);
      UTL_FILE.FCLOSE(log_file);
   EXCEPTION
      WHEN OTHERS THEN
      UTL_FILE.FCLOSE(log_file);
      RAISE;
   END write_log_info;
' LANGUAGE plpgsql;

窒息:

ERROR:  syntax error at or near "log_file"
LINE 6:       log_file UTL_FILE.FILE_TYPE

  CREATE OR REPLACE FUNCTION DB_SHELL_UTIL_PKG.DROP_DB_PRO (  p_db_name VARCHAR,
                  p_path VARCHAR,
                  p_file_name VARCHAR,
                  p_status VARCHAR  )
   RETURNS varchar as ' 
   DECLARE
    v_stmt  VARCHAR(1500);
  BEGIN
    v_stmt :=  'DROP USER '||p_db_name||' CASCADE';
    execute_stmt (v_stmt);
    p_status := 'USER '||p_db_name|| ' HAS BEEN DROPPED';
    v_stmt :=  'THE USER '||p_db_name||' HAS BEEN DROPPED';
    write_log_info(p_path, p_file_name, v_stmt);
    return(p_status);
  EXCEPTION
    WHEN OTHERS THEN
    v_stmt := 'EXCEPTION raised in DROP_DB_PRO';
    write_log_info (p_path, p_file_name, v_stmt);
    RAISE;
  END DROP_DB_PRO;
  ' LANGUAGE plpgsql;

非常感谢任何这些的帮助(我是功能/存储过程的新手)

2 个答案:

答案 0 :(得分:4)

对于DBMS_SQL,请查看Dynamic SQL in plpgsql

对于UTL_FILE,您必须超越plpgsqlOrafce

答案 1 :(得分:1)

Orafce可以UTL_FILE,只需要更新文档