In plpgSql, I need to add number to empty numeric array, this empty numeric array is variable.
What I do is this:
DECLARE
new_arr INTEGER[];
BEGIN
SELECT array_append(new_arr, 4) INTO new_arr;
This works, but I am not sure, may be exists better way for this?
You can do it, but this style is little bit obscure
use a assign statement, but you don't need a forget a correct initialization. In your example a new_arr doesn't hold a empty array. It holds a NULL. Do:
DECLARE new_arr int[] DEFAULT '{}';
BEGIN
new_arr := new_arr || 4;
-- or
new_arr := array_append(new_arr, 4);
-- both are equal
SELECT INTO
should be used if you do query to some relation.
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