Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

How do I create an array and then put a result of a function into it?
I'm doing it like this:

array text[];
select regexp_split_to_array('whatever this is it splits into array with spaces', E'\\s+')
    into array;

But obviously it doesn't work, when I try to

raise notice '%', array[1];

It's just not how it's done.

share|improve this question
    
You forgot to mention your version of Postgres, as well as what happens for you. Result? Error message? –  Erwin Brandstetter May 11 at 22:49

1 Answer 1

up vote 1 down vote accepted

array is a reserved word. You cannot use it as variable name in plpgsql. This works:

DO
$do$
DECLARE
   _arr text[];
BEGIN
SELECT regexp_split_to_array('thiiis splits into array with spaces', '\s+')
INTO _arr;

RAISE NOTICE '%', _arr[1];
END
$do$;

SQL Fiddle

Also simplified E'\\s+' -> '\s+'.
standard_conforming_strings = ON is the default since Postgres 9.1 and should be the universal setting by now.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.