2016-10-03 6 views
0

Oracleストアドプロシージャ(PL/SQL)をPostgreSQL(pl/pgsql)に移行する必要があります。私はそれを行う方法を理解することはできません。Oracle PL/SQLプロシージャをPostgreSQLに移行する方法

Procedure Check_File (strLine in varchar2 , lngRecord in number) is 
    type tab_str is table of varchar2(500) index by binary_integer; 
    tabline tab_str; 
    intp1  integer; 
    intp2  integer; 
    intsize integer; 
begin 

    -- Split line into table 
    intp1 := 1 ; 
    intp2 := instr (strline , ';' , intp1) ; 
    intsize := 0; 
    while intp2 != 0 loop 
    intsize := intsize + 1 ; 
    tabline(intsize) := trim(substr (strLine , intp1 , intp2-intp1)); 
    intp1 := intp2 + 1 ; 
    intp2 := instr (strline , ';' , intp1) ; 
    end loop; 
    intsize := intsize + 1 ; 
    tabline(intsize) := trim(substr (strLine , intp1)); 

    if intsize <> 23 then 
    utl_file.put_line (olog , 'Block - Record ' || lngRecord || ' - Incorrect number of fields') ; 
    raise oExcept; 
    end if; 
end; 

これは私がやったことです:

CREATE OR REPLACE FUNCTION Check_File (strLine in text , lngRecord in bigint) RETURNS VOID AS $body$ 
DECLARE 

    intp1  integer; 
    intp2  integer; 
    intsize integer; 


    -- CREATE TYPE tab_str AS (tab2 text[]); 
    tabline tab_str; 
BEGIN 

    -- Split line into table 
    intp1 := 1 ; 
    intp2 := instr (strline , ';' , intp1) ; 
    intsize := 0; 
    while intp2 != 0 loop 
     intsize := intsize + 1 ; 
     tabline(intsize) := trim(substring(strLine from intp1 for intp2-intp1)); 
     intp1 := intp2 + 1 ; 
     intp2 := instr (strline , ';' , intp1) ; 
    end loop; 
    intsize := intsize + 1 ; 
    tabline(intsize) := trim(substring(strLine from intp1)); 

    if intsize <> 23 then 
     utl_file.put_line (olog , 'Block - Record ' || lngRecord || ' - Incorrect number of fields') ; 
     raise oExcept; 
    end if; 

END; 
$body$ 
LANGUAGE PLPGSQL; 
+0

私は、単純な 'tabline:= string_to_array(strline、 ';')'を使って、Postgresで行の分割をはるかに簡単に行うことができると思います。あなたが 'utl_file'と同等のものを探しているのなら、orafceという拡張子を使うことができます:https://github.com/orafce/orafce –

答えて

0

は非常に簡単に思えます。

  • 何も返す必要がない場合は、RETURNS voidの関数を定義します。
  • tablinetext[](配列text)と宣言します。ドキュメントhow to handle arraysを参照してください。
  • adminpack contribモジュールの関数を使用して、データベースサーバー上のファイルに書き込むことができます。

The documentationは、遠慮なく質問を解決するのに役立ちます。

+0

あなたの答えに感謝します。私はそれを行っているが、タブラインの構文 "tabline(intsize)"にはまだ問題がある。 – Kamfasage

+0

タブラインのテキスト[]; 'を宣言する必要があります。私は答えを少し広げました。コードをPostgreSQLの配列構文に変更する必要があります。 –

+0

はい、問題が何だったのか分かりました。タブラインは配列でなければなりません。 tabline [intsize]:= trim(substring(intp2-intp1の場合はintp1からのstrLine)); 私はもはや構文エラーがありません。 ありがとう – Kamfasage

関連する問題