Re: greetings

From: Ian Lance Taylor <ian(at)airs(dot)com>
To: Ken Kline <ken(at)oldbs(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-sql(at)postgresql(dot)org
Subject: Re: greetings
Date: 2001-02-24 23:15:10
Message-ID: siwvafwu4h.fsf@daffy.airs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Ken Kline <ken(at)oldbs(dot)com> writes:

> it is to be server side code....
> the code I gave you was merely an example
> of a cursor that I found when I did a search...
> http://www.armed.net/how/pg001676.htm

I think Tom addressed how to solve your actual problem.

I'll just comment that the example you see is of pure SQL being passed
in from a client. If you want to use a loop, you need to use
PL/pgSQL. And PL/pgSQL doesn't currently support cursors.

> DECLARE
> CURSOR get_rows AS
> SELECT DISTINCT pseason, pyear FROM load_members
> WHERE pyear IS NOT NULL
> AND pseason IS NOT NULL
> ORDER BY pyear, pseason;
> BEGIN
> FOR rec IN get rows LOOP
> INSERT INTO pledge_classes (semester, year)
> VALUES
> (rec.pseason, rec.pyear);
> END LOOP;
> COMMIT;
> END;

You can do this in PL/pgSQL by just doing the FOR over the SELECT.
This is untested, but it shows the basic idea.

DECLARE
rec RECORD;
BEGIN
FOR rec IN SELECT DISTINCT pseason, pyear FROM load_members
WHERE pyear IS NOT NULL
AND pseason IS NOT NULL
ORDER BY pyear, pseason;
LOOP
INSERT INTO pledge_classes (semester, year)
VALUES (rec.pseason, rec.pyear);
END LOOP;
COMMIT;
END;

But as Tom said there is no reason to sort records when inserting
them, since it doesn't make any difference what order they are in in
the table.

Ian

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Roberto Mello 2001-02-25 01:17:16 Passing a table to PL/pgSQL
Previous Message Tom Lane 2001-02-24 22:02:40 Re: greetings