【发布时间】: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