无法在pl/pgsql中创建过程,但代码在没有创建过程的情况下工作

问题描述:

我一直在尝试创建一个过程以便“EXECUTEINSERT INTO”表格。我放弃了这一点,并继续尝试动态生成所需的代码插入。无法在pl/pgsql中创建过程,但代码在没有创建过程的情况下工作

我已经通过不创建程序并简单地从“DECLARE”位开始解决了我的问题;但仍然没有设法使pl/pgsql过程正常工作。

以下过程不工作:

CREATE PROCEDURE populate_xcc_allocatable() AS 
    $body$ 
     DECLARE 
      TYPE tablearray IS VARRAY(17) OF VARCHAR2(30); 
      xa_tables tablearray := tablearray('allocation', 'container', 'location', 'sap_posting', 'status'); 
      total integer; 
     BEGIN 
      total := xa_tables.count; 
      FOR i IN 1..total 
       LOOP 
        dbms_output.put_line('INSERT INTO allocatable VALUES (nextval(''allocatable_id_seq''), ''' || xa_tables(i) || ''');'); 
       END LOOP; 
     END; 
    $body$ 
LANGUAGE plpgsql; 


LINE 4: TYPE tablearray IS VARRAY(17) OF VARCHAR2(30); 
CONTEXT: invalid type name "tablearray IS VARRAY(17) OF VARCHAR2(30)" 

但是,这是工作的罚款:

DECLARE 
    TYPE tablearray IS VARRAY(17) OF VARCHAR2(30); 
    xa_tables tablearray := tablearray('allocation', 'container', 'location', 'spirit_portion', 'activity', 'asset_ownership', 'container_location', 'sap_posting', 'status'); 
     total integer; 
    BEGIN 
     total := xa_tables.count; 
     FOR i IN 1..total 
      LOOP 
       dbms_output.put_line('INSERT INTO xcc_allocatable VALUES (nextval(''xcc_allocatable_id_seq''), ''' || xa_tables(i) || ''');'); 
      END LOOP; 
    END; 

更多帮助10个PostgreSQL的没有程序,但函数返回 void代替:

CREATE FUNCTION populate_xcc_allocatable() RETURNS void AS $body$ 

没有 '本地型',用array type代替:

DECLARE 
    xa_tables text[] := array[ 
    'allocation', 'container', 'location', 
    'spirit_portion', 'activity', 'asset_ownership', 
    'container_location', 'sap_posting', 'status']; 
    total integer; 
    i integer; -- Loop variables should be explicitly declared 

为了得到阵列测量使用array functions

BEGIN 
    total := array_length(xa_tables, 1); 
    FOR i in 1 .. total LOOP 
    raise info 'INSERT INTO allocatable VALUES (nextval(''allocatable_id_seq''), ''%'');', xa_tables[i]; 
    END LOOP; 

任何功能都应该由RETURN完成:

INSERT INTO allocatable 
    SELECT nextval('allocatable_id_seq'), x 
    FROM unnest(array[ 
    'allocation', 'container', 'location', 
    'spirit_portion', 'activity', 'asset_ownership', 
    'container_location', 'sap_posting', 'status']) as t(x); 
:10
RETURN; 
END $body$ language plpgsql; 

最后,你要创建的功能可以通过纯SQL替代

dbms_output.put_line是甲骨文。和declare是甲骨文anonymous PL/SQL block

Postgres的你应该使用raise info '%','some text';dbms_output.put_line('some text');

和,而不是匿名PL/SQL块使用do语句来代替,像

do 
$$ 
declare 
begin 
end; 
$$ 
; 

坦率地说,我想你如果您将标签postgres更改为oracleplpgsqlplsql ...