1

I have been trying to create a procedure in order to "EXECUTE INSERT INTO" a table. I gave up on this and continued trying to dynamically generate the required insert into code.

I have solved my issue by not creating the procedure and simply starting from the "DECLARE" bit; but still have not managed to make the pl/pgsql procedure to work.

The following procedure is not working:

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)"

But this is working fine:

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;

2 Answers 2

3

dbms_output.put_line is oracle. and declare is oracle anonymous PL/SQL block

for postgres you should use raise info '%','some text'; instead of dbms_output.put_line('some text');

and instead of anonymous PL/SQL block use do statement, like

do
$$
declare
begin
end;
$$
;

to be frank, I think you get more help if you change tag postgres to oracle and plpgsql to plsql...

Sign up to request clarification or add additional context in comments.

Comments

2

PostgreSQL have not procedures but functions returns void instead:

CREATE FUNCTION populate_xcc_allocatable() RETURNS void AS $body$

There is no 'local types', use array type instead:

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

To get array measurements use 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;

Any function should be finished by the RETURN:

  RETURN;
END $body$ language plpgsql;

Finally, the function you are trying to create can be replaced by the pure SQL:

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.