By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,691 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,691 IT Pros & Developers. It's quick & easy.

Declaring empty, non-NULL array in plpgsql

P: n/a
CSN
Is there a way to declare an array in plpgsql so that
it's empty (not NULL)? The following causes arr to end
up being NULL:

arr varchar[];
-- do stuff with arr..
arr = array_append(arr, '','');
And this causes an unwanted element at the front:

arr varchar[] := array[''''];
-- do stuff with arr..
arr = array_append(arr, '','');
__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
CSN <co*******************@yahoo.com> writes:
Is there a way to declare an array in plpgsql so that
it's empty (not NULL)?


Sure. An empty-array literal is '{}', so:

regression=# create function foo(int) returns varchar[] as '
regression'# declare
regression'# arr varchar[] := ''{}'';
regression'# begin
regression'# for i in 1..$1 loop
regression'# arr = array_append(arr, i::varchar);
regression'# end loop;
regression'# return arr;
regression'# end' language plpgsql;
CREATE FUNCTION
regression=# select foo(10);
foo
------------------------
{1,2,3,4,5,6,7,8,9,10}
(1 row)
regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.