Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
General
cancel
Showing results for 
Search instead for 
Did you mean: 

postgres - psql error when using a for loop

Kevin Nikiforuk
Valued Contributor

postgres - psql error when using a for loop

Hi there...I've got PostgreSQL A.06.00-8.1.3.1 installed on my server and I'm trying to create a for loop:

for LV in 1..10 LOOP

select * from ldev-rg where rg='$lv';

END LOOP;

but when I run it, I get:

psql:blah.sql:3: ERROR: syntax error at or near "for" at character 1
psql:blah.sql:3: LINE 1: for LV in 1..10 LOOP
psql:blah.sql:3: ^
psql:blah.sql:5: ERROR: syntax error at or near "LOOP" at character 5
psql:blah.sql:5: LINE 1: END LOOP;
psql:blah.sql:5: ^

Am I totally missing something here?
5 REPLIES
spex
Honored Contributor

Re: postgres - psql error when using a for loop

Ken,

I don't have much plpgsql experience, but try this:

FOR LV in 1..10 LOOP
SELECT * FROM ldev-rg WHERE rg="$LV";
END LOOP;


PCS
Kevin Nikiforuk
Valued Contributor

Re: postgres - psql error when using a for loop

Same thing's happening...

xp512-0715-0716=# FOR LV in 1..10 LOOP
xp512-0715-0716-# SELECT * FROM ldev-rg WHERE rg="$LV";
ERROR: syntax error at or near "FOR" at character 1
LINE 1: FOR LV in 1..10 LOOP
^
Peter Godron
Honored Contributor

Re: postgres - psql error when using a for loop

Do you have to declare LV?
spex
Honored Contributor

Re: postgres - psql error when using a for loop

How about just:

SELECT COUNT(*) FROM ldev-rg;

PCS
Kevin Nikiforuk
Valued Contributor

Re: postgres - psql error when using a for loop

My mistake, the table name is actually ldevrg, not ldev-rg.

xp512-0715-0716=# select count(*) from ldevrg;
count
-------
1491
(1 row)