Thread: How to iterate through arrays?

How to iterate through arrays?

From
NosyMan
Date:
Hi there,

I'm trying to iterate through arrays in PL/PGSQL:
....DECLARE    update_query CHAR;    update_query_params CHAR ARRAY[6];BEGIN        update_query_params[1]:='some
text';                       RAISE NOTICE 'Testing element %', update_query_params[1];END
 
.....


It does not 'compile'... :-(. Can you tell me what is the problem?

Thanks,
Nosy


Re: How to iterate through arrays?

From
PFC
Date:
- use TEXT instead of CHAR (what is CHAR without (n) ?)
- inintialize your array with '{}' because it is created as NULL if you  
just declare it without setting it to an empty array.

> Hi there,
>
> I'm trying to iterate through arrays in PL/PGSQL:
> ....
>     DECLARE
>         update_query CHAR;
>         update_query_params CHAR ARRAY[6];
>     BEGIN
>             update_query_params[1]:='some text';        
>             
>             RAISE NOTICE 'Testing element %', update_query_params[1];
>     END
> .....
>
>
> It does not 'compile'... :-(. Can you tell me what is the problem?
>
> Thanks,
> Nosy
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>




Re: How to iterate through arrays?

From
NosyMan
Date:
I think the problem is not there. I got the following error:
'ERROR:  syntax error at or near "[" at character 1234', the line is: RAISE
NOTICE '% ...',update_query_params[1];

May be someone give a code sample...

Thanks




Pe data de Mie 09 Feb 2005 16:16, ati scris:
> - use TEXT instead of CHAR (what is CHAR without (n) ?)
> - inintialize your array with '{}' because it is created as NULL if you  
> just declare it without setting it to an empty array.


Re: How to iterate through arrays?

From
John DeSoi
Date:
On Feb 9, 2005, at 9:26 AM, NosyMan wrote:

> I think the problem is not there. I got the following error:
> 'ERROR:  syntax error at or near "[" at character 1234', the line is: 
> RAISE
> NOTICE '% ...',update_query_params[1];

The problem here is RAISE, NOTICE, etc. can only use simple variables 
in the format string. Try it like:

myTextVar := update_query_params[1];
NOTICE '% ...',myTextVar;


John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL