1

My pgScript script cannot read values from an array of string. This is my script.

DECLARE @my_array;
DECLARE @i;


SET @my_array = '{"element 1", "element 2",
"element 3", "element 4", "element 5", "element 6",
"element 7"}';
SET @i = 1;
PRINT 'TEST1';
WHILE @i < 8
BEGIN
PRINT @my_array[@i];
SET @i = @i + 1;
PRINT 'i = ' + cast(@i AS STRING);
END

It prints all values of i (from 2 to 8) but the values of @my_array[@i] are empty. Thank you for your help.

  • I found this syntax here (https://www.pgadmin.org/docs/dev/pgscript.html). I use pgadmin to execute the script – user7069441 Oct 25 '16 at 12:44
  • Why are you using pgscript at all? This can be done using simple statement: `select * from unnest('{"element 1", "element 2","element 3", "element 4", "element 5", "element 6", "element 7"}'::text[]) with ordinality as x(element, i)` –  Oct 25 '16 at 14:21
  • `SET @my_array = '("element 1","element 2","element 3","element 4","element 5","element 6","element 7")';` then `SET @my_array = CAST(@my_array AS RECORD);` and finally `PRINT @my_array[0][@i];` – Abelisto Oct 25 '16 at 18:00

0 Answers0