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'];
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.
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.
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.
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.
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.
@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;
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With