Re: foreach in sql functions

From: Richard Huxton <dev(at)archonet(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Re: foreach in sql functions
Date: 2002-10-03 08:34:04
Message-ID: 200210030934.05374.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wednesday 02 Oct 2002 11:16 pm, Zac Hillier wrote:
>
> Is it possible to write sql functions with a foreach style loop?
>
> CREATE FUNCTION crt_bsk_dtl(integer) RETURNS integer AS '
> SELECT PrdKey FROM Prd WHERE Sts < 999;
> foreach(Prd.PrdKey){
> INSERT INTO BskDtl (BskKey, PrdKey) VALUES ($1, Prd.PrdKey);
> }
> SELECT 1;
> ' LANGUAGE SQL

Other people are suggesting plpgsql, but for this case you can use SQL:

INSERT INTO BskDtl (BskKey,PrdKey) SELECT $1,PrdKey FROM Prd WHERE Sts < 999;

I realise in a more general case you might want to manipulate data as you loop
though.

- Richard Huxton

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Patrick Fiche 2002-10-03 08:35:36 Re: foreach in sql functions
Previous Message Zac Hillier 2002-10-03 08:24:43 Re: foreach in sql functions