Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an array of rowtype in a PostgreSQL function?

I am trying to create a PostgreSQL function where I will loop over the rows of a query and store some of them in an array, before doing more stuff with it. How can I create an array of rowtype?

CREATE OR REPLACE FUNCTION forExample() RETURNS integer AS
$BODY$
DECLARE
    r "WEBHOST"%rowtype;
    b "WEBHOST"%rowtype[];   <- This does not work !!!
BEGIN
    FOR r IN SELECT * FROM "WEBHOST"
    LOOP
        array_append(b, r);
    END LOOP;
    RETURN 33;
END
$BODY$
LANGUAGE 'plpgsql';

The above function will be more complex, but I am providing a simplified version for this question.

like image 865
Jérôme Verstrynge Avatar asked Mar 10 '14 17:03

Jérôme Verstrynge


People also ask

How do I declare an array in PostgreSQL?

You can easily create arrays in PostgreSQL by adding square brackets [] immediately after the data type for the column. create table employees ( first_name varchar, last_name varchar, phone_numbers integer[] ); In the above example, we have created column phone_numbers as an array of integers.

How do I create a Rowtype variable in PostgreSQL?

A row variable can be declared to have the same type as the rows of an existing table or view, by using the table_name %ROWTYPE notation; or it can be declared by giving a composite type's name.

Can you insert array in PostgreSQL?

PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays. Arrays of any built-in or user-defined base type, enum type, composite type, range type, or domain can be created.

How do I return a set of records in PostgreSQL?

Let's make a function that returns all the rows of a table whose name you pass in as a parameter. create or replace function GetRows(text) returns setof record as ' declare r record; begin for r in EXECUTE ''select * from '' || $1 loop return next r; end loop; return; end ' language 'plpgsql';


1 Answers

CREATE OR REPLACE FUNCTION for_example()
  RETURNS integer AS
$func$
DECLARE
   r "WEBHOST";
   b "WEBHOST"[];         -- this works
BEGIN
   FOR r IN 
      SELECT * FROM "WEBHOST"
   LOOP
      b := b || r;        -- this, too
   END LOOP;

   RAISE NOTICE '%', b;   -- get feedback
   RETURN 33;
END
$func$  LANGUAGE plpgsql; -- and lose the quotes

%rowtype is generally not necessary. Normally, the associated rowtype of a table is available as type of the same name.

And do not quote the language name.

And you cannot just have stand-alone function calls in plpgsql. Using an assignment instead.

It's also not a good idea to use CaMeL-case identifiers in Postgres. Use legal, lower-case identifiers to make your life easier.

The best for last: This can be much simpler with the aggregate function array_agg():

CREATE OR REPLACE FUNCTION for_example()
  RETURNS integer AS
$func$
DECLARE
   b "WEBHOST"[];
BEGIN
   SELECT array_agg(tbl) INTO b FROM "WEBHOST" tbl;       

   RAISE NOTICE '%', b;
   RETURN 33;
END
$func$  LANGUAGE plpgsql;
like image 67
Erwin Brandstetter Avatar answered Sep 29 '22 00:09

Erwin Brandstetter