17

I am trying to create a simple PostgreSQL function, where by using INT parameter I like to get array back. The example below will not work, but shall give idea of what I try to get back from a function. Thanks.

CREATE OR REPLACE FUNCTION contact_countries_array(INT)
  RETURNS ANYARRAY AS '
  SELECT ARRAY[contacts_primarycountry, contacts_othercountry] FROM contacts WHERE contacts_id = $1'
  LANGUAGE SQL;

The data type of contacts_primarycountry and contacts_othercountry is integer. contacts_id is unique and integer.

2
  • Please edit your question add some sample data and the expected output based on that data. I have no idea what you are trying to do Commented Feb 13, 2016 at 0:31
  • you are right, I forgot to describe wished data types Commented Feb 13, 2016 at 7:16

4 Answers 4

16

Per the docs:

It is permitted to have polymorphic arguments with a fixed return type, but the converse is not.

As such, I think your attempt to return anyarray won't work.

Your fields look like text, so I think if you altered it to something like this, it would work:

CREATE OR REPLACE FUNCTION contact_countries_array(INT)
RETURNS text[] AS $$
  select array[contacts_primarycountry::text, contacts_othercountry::text]
  FROM contacts WHERE contacts_id = $1
$$
LANGUAGE SQL;

This should compile, and it might work, but I'm honestly not sure:

CREATE OR REPLACE FUNCTION contact_countries_array(anyelement)
RETURNS anyarray AS $$
  select array[contacts_primarycountry::text, contacts_othercountry::text]
  FROM contacts WHERE contacts_id = $1
$$
LANGUAGE SQL;

I think the datatypes would have to match perfectly for this to work, unless you did casting.

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

4 Comments

Yes to the first variant, assuming that contacts.contacts_id is defined UNIQUE. The polymorphic type in the second one is probably unwarranted as the data type of contacts.contacts_id should be known in advance anyway.
Thank you people. Based on @Hambone, I have tried it like this: CREATE OR REPLACE FUNCTION contact_countries_array(ANYELEMENT) RETURNS ANYARRAY AS ' SELECT ARRAY[contacts_primarycountry::int, contacts_othercountry::int] FROM contacts WHERE contacts_id = $1' LANGUAGE SQL; and now it works. It returns like {55,81} as 2 possible countries of the contact, that I can compare now to list of countries with {55,81} && {55,82,101,12}, @Hambone, thank you much, @a_horse_with_no_name, @erwin_brandstetter
Doesn't it require a return inside function, for it to be a valid code?
@Eugene -- not when it's LANGUAGE SQL. You're probably thinking pgplsql
10

Declaring Array, Looping, Adding items to Array, Returning Array with Postgres Function,

You can declare INTEGER array instead of TEXT and avoid casting (counter::TEXT) as well as return type TEXT[]. (Added those for reference.)

CREATE OR REPLACE FUNCTION "GetNumbers"(maxNo INTEGER) RETURNS TEXT[] AS $nums$

DECLARE 
    counter INTEGER := 0;
    nums TEXT[] := ARRAY[]::TEXT[];

BEGIN
    LOOP
        EXIT WHEN counter = maxNo;
        counter = counter + 1;
        nums = array_append(nums, counter::TEXT);
    END LOOP;

    RETURN nums;
END ;

$nums$ LANGUAGE plpgsql;

SELECT "GetNumbers"(5); -- {1,2,3,4,5}

2 Comments

The script does not work. Here is an updated version. CREATE OR REPLACE FUNCTION get_numbers(maxNo INTEGER) RETURNS TEXT[] AS $$ DECLARE counter INTEGER := 0; nums TEXT[] := ARRAY[]::TEXT[]; BEGIN LOOP EXIT WHEN counter = maxNo; counter = counter + 1; nums = array_append(nums, counter::TEXT); END LOOP; RETURN nums; END ; $$ LANGUAGE plpgsql;
Thank you nick, I updated the function. Now it's correct.
0

For example, you can create my_func() which can return VARCHAR[] type as shown below. *You can replace VARCHAR[] with TEXT[].:

CREATE FUNCTION my_func() RETURNS VARCHAR[]
AS $$                          -- ↑ ↑ ↑ ↑ ↑
SELECT ARRAY['John','David','Robert'];
$$ LANGUAGE SQL;

Then, calling my_func() returns an array as shown below:

postgres=# SELECT my_func();
       my_func
---------------------
 {John,David,Robert}
(1 row)

Comments

0

With plpgsql it's slightly different. So for future visitors, here is a sample:

CREATE OR REPLACE FUNCTION public.split_something(des_in text)
RETURNS text[]
 LANGUAGE plpgsql
AS $function$
declare
   p1 text;
   p2 text;
begin
     ---other logic to set values for p1/p2
return array[p1::text, p2::text];
END;
$function$;

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.