pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Question about SQL Control Structure(if then, for loop)
4+ messages / 3 participants
[nested] [flat]

* Question about SQL Control Structure(if then, for loop)
@ 2006-05-17 01:51  Jay Chiu <jaychiu@ureach.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Jay Chiu @ 2006-05-17 01:51 UTC (permalink / raw)
  To: pgsql-sql; +Cc: jaychiu@ureach.com

Hello,

I am new to PostgreSQL and want to use the IF-THEN; LOOP
statements in my SQL command file. Thus I may use psql -f cfile
to exceute.

However I always got syntax error on if/for in psql. For
example: 
RDM=# for i in 1 .. 10 loop
RDM-# select "test"
RDM-# end loop;
ERROR:  syntax error at or near "for" at character 1
LINE 1: for i in 1 .. 10 loop

RDM=# if exits ( select * from testtable)
RDM-# then
RDM-# select "TEST"
RDM-# ;
ERROR:  syntax error at or near "if" at character 1
LINE 1: if exits ( select * from testtable)
        ^

Can someone tell me how to use if/for in psql? The document
shows the control structures in the PL/pgSQL section. Must I use
if/for inside some procedure/function?

Thanks a lot.

Jay


________________________________________________
Get your own "800" number
Voicemail, fax, email, and a lot more
http://www.ureach.com/reg/tag



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Question about SQL Control Structure(if then, for loop)
@ 2006-05-18 06:12  A. Kretschmer <andreas.kretschmer@schollglas.com>
  parent: Jay Chiu <jaychiu@ureach.com>
  1 sibling, 0 replies; 4+ messages in thread

From: A. Kretschmer @ 2006-05-18 06:12 UTC (permalink / raw)
  To: pgsql-sql

am  16.05.2006, um 21:51:45 -0400 mailte Jay Chiu folgendes:
> Can someone tell me how to use if/for in psql? The document

In plain SQL isn't if, then, else and so on.


> shows the control structures in the PL/pgSQL section. Must I use
> if/for inside some procedure/function?

Yes.


HTH, Andreas
-- 
Andreas Kretschmer    (Kontakt: siehe Header)
Heynitz:  035242/47215,      D1: 0160/7141639
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net
 ===    Schollglas Unternehmensgruppe    === 



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Question about SQL Control Structure(if then, for loop)
@ 2006-05-18 09:19  Michael Artz <mlartz@gmail.com>
  parent: Jay Chiu <jaychiu@ureach.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Michael Artz @ 2006-05-18 09:19 UTC (permalink / raw)
  To: jaychiu@ureach.com; +Cc: pgsql-sql

If you're control is that simple, you can write similar statements in pure
SQL:

RDM=# for i in 1 .. 10 loop
> RDM-# select "test"
> RDM-# end loop;
> ERROR:  syntax error at or near "for" at character 1
> LINE 1: for i in 1 .. 10 loop


SELECT 'test' FROM


RDM=# if exits ( select * from testtable)
> RDM-# then
> RDM-# select "TEST"
> RDM-# ;
> ERROR:  syntax error at or near "if" at character 1
> LINE 1: if exits ( select * from testtable)
>         ^


 SELECT 'test' FROM testtable LIMIT 1;

Without knowing about what you want to do, I can't guarantee that that will
suffice and/or be efficient.  If it gets much more complicated, you might
have to go to some procedural language (PL/PGSQL, PL/Perl, etc).  Just
remember that SQL is set-based, not procedural.

-Mike

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Question about SQL Control Structure(if then, for loop)
@ 2006-05-18 09:21  Michael Artz <mlartz@gmail.com>
  parent: Michael Artz <mlartz@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Michael Artz @ 2006-05-18 09:21 UTC (permalink / raw)
  To: jaychiu@ureach.com; +Cc: pgsql-sql

> SELECT 'test' FROM
>

SELECT 'test' FROM generate_series(1,10);

Sorry, thought faster than I could type :)

-Mike

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2006-05-18 09:21 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-05-17 01:51 Question about SQL Control Structure(if then, for loop) Jay Chiu <jaychiu@ureach.com>
2006-05-18 06:12 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
2006-05-18 09:19 ` Michael Artz <mlartz@gmail.com>
2006-05-18 09:21   ` Michael Artz <mlartz@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox