Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a function declaring a predefined text array

I need to create a function in Postgres and one of the variables I declare is a predefined text array, but I don't know the syntax to set its values. This is what I have so far:

CREATE OR REPLACE FUNCTION testFunction() RETURNS text
AS $$
DECLARE
    TESTARRAY TEXT['value 1', 'value 2', 'value 3'];
BEGIN
    return 'any text';
END;
$$ LANGUAGE 'plpgsql';

I get this error when I execute the code:

ERROR:  syntax error at or near "'value 1'"
LINE 5: TESTARRAY TEXT['value 1', 'value 2', 'value 3'];
like image 260
Edson Horacio Junior Avatar asked Jul 24 '14 15:07

Edson Horacio Junior


People also ask

How do you declare an array declaration?

The syntax for declaring an array is: datatype[] arrayName; datatype : The type of Objects that will be stored in the array eg. int , char etc.

How do I create an array of text in PostgreSQL?

In this operation, we can insert a String Array into the table using the following syntax. In the above syntax where insert into is a keyword, column name1, column name2 is a specified column name in the table, and values mean actual string array values which we need to insert into the table.

How do I declare an array in VBA?

Use a Static, Dim, Private, or Public statement to declare an array, leaving the parentheses empty, as shown in the following example. Use the ReDim statement to declare an array implicitly within a procedure. Be careful not to misspell the name of the array when you use the ReDim statement.

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.

What is text [] in PostgreSQL?

PostgreSQL supports a character data type called TEXT. This data type is used to store character of unlimited length. It is represented as text in PostgreSQL. The performance of the varchar (without n) and text are the same. Syntax: variable_name TEXT.


2 Answers

@pozs already gave a proper answer.

In addition, when in doubt about proper syntax, you can just ask Postgres for the string literal:

test=# SELECT ARRAY['value 1', 'value 2', 'value 3'];
              array
---------------------------------
 {"value 1","value 2","value 3"}

test=# SELECT  ARRAY['foo', 'bar', 'b A "Z'];
        array
---------------------
 {foo,bar,"b A \"Z"}

There is a string representation for every possible value of every type.

To get the readily quoted version, that deals with all possible corner cases, wrap it in quote_nullable():

test=# SELECT quote_nullable(ARRAY['value 1', 'value 2', 'value 3']);
          quote_nullable
-----------------------------------
 '{"value 1","value 2","value 3"}'

test=# SELECT quote_nullable(ARRAY['foo', 'bar', 'b ''A'' "Z"']);
         quote_nullable
--------------------------------
 E'{foo,bar,"b ''A'' \\"Z\\""}'

Your example:

CREATE OR REPLACE FUNCTION test_function()
  RETURNS text AS
$func$
DECLARE
   testarray text[] := '{"value 1","value 2","value 3"}';
BEGIN
   RETURN 'any text';
END
$func$ LANGUAGE plpgsql;

Other points

  • Don't quote the language name: LANGUAGE plpgsql.
  • Use lower case identifiers in Postgres. Per documentation:

    All key words are case-insensitive. Identifiers are implicitly converted to lower case unless double-quoted, just as they are in ordinary SQL commands.

like image 42
Erwin Brandstetter Avatar answered Sep 22 '22 20:09

Erwin Brandstetter


The right syntax for default value (in a variable declaration) is { DEFAULT | := } expression.

For expression, you can use any of the array inputs.

F.ex. these can work:

DECLARE
  test1  TEXT ARRAY  DEFAULT  ARRAY['value 1', 'value 2', 'value 3'];
  test2  TEXT[]      :=       '{"value 1", "value 2", "value 3"}';
  test3  TEXT[]      DEFAULT  ARRAY[]::TEXT[]; -- empty array-constructors need a cast
like image 126
pozs Avatar answered Sep 21 '22 20:09

pozs