pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedHelp me do a LOOP
2+ messages / 2 participants
[nested] [flat]
* Help me do a LOOP
@ 2005-12-14 08:21 Calin Meze <mezecalin@yahoo.com>
0 siblings, 1 reply; 2+ messages in thread
From: Calin Meze @ 2005-12-14 08:21 UTC (permalink / raw)
To: pgsql-sql
I need to test each record of a cursor for some requirements, something like this
DECLARE xpvnr varchar(50);
xdata date;
xcod int;
xagentd varchar(3);
xid bigint;
xserie varchar(4);
xnr varchar(7);
xsocasig varchar(3);
DECLARE myCursor CURSOR FOR
select cod,pvnr,data,agentd,id,serie,nr,socasig
from pvmst, pvdtl
where pvmst.cod=pvdtl.cod
order by data;
BEGIN
delete from stoccalc_temp;
OPEN myCursor;
FETCH NEXT FROM myCursor INTO xcod,xpvnr,xdata,xagentd,xid,xserie,xnr, xsocasig;
WHILE (still in the cursor)
BEGIN
DECLARE
xxcod varchar(21);
xxcod2 varchar(21);
xxagent varchar(3);
xxid bigint;
DECLARE my2cursor CURSOR FOR
select cod,agent,id from stoccalc_temp
where nr=xnr and serie=xserie and socasig=xsocasig;
OPEN my2cursor;
FETCH NEXT FROM my2cursor INTO xxcod,xxagent,xxid;
xxcod2:=TRIM('2005'+TRIM(xsocasig)+TRIM(xxagent)+TRIM(xserie)+TRIM(xnr));
IF xxcod <> '' THEN -- inseamna ca este in stoccalc_temp polita
update stoccalc set cod=xxcod2, agent=xxagent, stare='0' where id=xid;
END IF;
CLOSE my2cursor;
FETCH NEXT FROM myCursor INTO xcod,xpvnr,xdata,xagentd,xid,xserie,xnr,xsocasig;
END;
CLOSE myCursor;
END;
-- the problem is that I do not know how to make the while loop execute until the last record "WHILE (still in the cursor)"... So I need something like
for i:=1 to lastrecord_of_cursor do
begin
...
end;
Can anyone help me with this? It really seems silly... but I cannot get to solve this, I do not know where to look for syntax like this or tutorials like this.
Help
---------------------------------
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Help me do a LOOP
@ 2005-12-21 09:34 Richard Huxton <dev@archonet.com>
parent: Calin Meze <mezecalin@yahoo.com>
0 siblings, 0 replies; 2+ messages in thread
From: Richard Huxton @ 2005-12-21 09:34 UTC (permalink / raw)
To: Calin Meze <mezecalin@yahoo.com>; +Cc: pgsql-sql
Calin Meze wrote:
> I need to test each record of a cursor for some requirements,
> something like this
[snip]
> -- the problem is that I do not know how to make the while loop
> execute until the last record "WHILE (still in the cursor)"... So I
> need something like
>
>
> Can anyone help me with this? It really seems silly... but I cannot
> get to solve this, I do not know where to look for syntax like this
> or tutorials like this.
Assuming this is plpgsql you are probably looking for the FOUND
pseudo-variable. See Ch 36.6.6 Obtaining the Result Status
You don't actually need to bother with a cursor in plpgsql, just loop
over a SELECT (Ch 36.7.4 Looping Through Query Results). That basically
*is* a cursor internally.
--
Richard Huxton
Archonet Ltd
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2005-12-21 09:34 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-12-14 08:21 Help me do a LOOP Calin Meze <mezecalin@yahoo.com>
2005-12-21 09:34 ` Richard Huxton <dev@archonet.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