Re: [SQL] Looking for information on PostgreSQL Stored Procedures

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Douglas McNaught <doug(at)mcnaught(dot)org>
Cc: "Foster, Stephen" <stephenlfoster(at)comcast(dot)net>, pgsql-general(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] Looking for information on PostgreSQL Stored Procedures
Date: 2005-12-11 17:22:18
Message-ID: 20051211172218.GA87286@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

On Sun, Dec 11, 2005 at 11:38:47AM -0500, Douglas McNaught wrote:
> "Foster, Stephen" <stephenlfoster(at)comcast(dot)net> writes:
>
> > WHILE (--Lost on variable name for end of query; EmptyQueryResponse <>
> > 0? --)
> > BEGIN
> > IF LastName = fname THEN
> > DELETE FROM MailingList WHERE id = id;
> > END IF;
> > LastName := fname;
> > FETCH NEXT FROM NewListCursor INTO fname, id;
> > END;
> > CLOSE NewListCursor;
> > $BODY$
> > LANGUAGE 'sql' VOLATILE;
>
> You can't do any looping or other control structures in an SQL
> function. Use PL/pgSQL instead.

And as I mentioned in my previous post, you can loop through query
results without messing around with an explicit cursor.

CREATE FUNCTION testfunc() RETURNS void AS $$
DECLARE
row record;
BEGIN
FOR row IN SELECT * FROM tablename ORDER BY whatever LOOP
-- do stuff that refers to row.column_name
END LOOP;

RETURN;
END;
$$ LANGUAGE plpgsql;

--
Michael Fuhr

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Foster, Stephen 2005-12-11 17:47:29 Re: [SQL] Looking for information on PostgreSQL Stored Procedures
Previous Message Michael Fuhr 2005-12-11 17:04:38 Re: TSearch2: Auto identify document language?

Browse pgsql-sql by date

  From Date Subject
Next Message Foster, Stephen 2005-12-11 17:47:29 Re: [SQL] Looking for information on PostgreSQL Stored Procedures
Previous Message Tom Lane 2005-12-11 17:04:18 Re: [SQL] Looking for information on PostgreSQL Stored Procedures