【问题标题】:How to migrate an Oracle PL/SQL procedure to PostgreSQL如何将 Oracle PL/SQL 过程迁移到 PostgreSQL
【发布时间】:2016-10-03 13:29:15
【问题描述】:

我需要将 Oracle 存储过程 (PL/SQL) 迁移到 PostgreSQL (pl/pgsql)。我不知道该怎么做。

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;

【问题讨论】:

  • 我认为在 Postgres 中使用简单的tabline := string_to_array(strline, ';') 可以更轻松地完成行的拆分。如果您正在寻找与utl_file 等效的版本,您可以使用扩展名 orafce:github.com/orafce/orafce

标签: oracle postgresql plpgsql


【解决方案1】:

看起来很简单。

  • 如果您不需要返回任何内容,请使用 RETURNS void 定义一个函数。
  • 将tabline 声明为text[](text 的数组)。请参阅文档how to handle arrays。
  • 使用adminpack contrib 模块中的函数写入数据库服务器上的文件。

The documentation 将帮助您解决问题。

【讨论】:

  • 感谢您的回答。我已经这样做了,但制表符语法“tabline(intsize)”仍然存在问题..
  • 你应该DECLARE tabline text[];。我扩大了答案。您必须将代码更改为 PostgreSQL 数组语法。
  • 是的,我已经弄清楚问题出在哪里了。tabline 必须是一个数组。所以我这样做了:tabline [ intsize ] := trim(substring(strLine from intp1 for intp2-intp1 ));我不再有语法错误了。谢谢
猜你喜欢
  • 1970-01-01
  • 2012-08-06
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 2011-01-17
  • 2019-10-16
相关资源
最近更新 更多