pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedSelect into table%ROWTYPE failed
3+ messages / 2 participants
[nested] [flat]
* Select into table%ROWTYPE failed
@ 2018-09-18 16:14 marcelo <marcelo.nicolet@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: marcelo @ 2018-09-18 16:14 UTC (permalink / raw)
To: pgsql-general
I´m testing a trigger function in a 9.4 installation.
It´s for bill number assignation, but with a twist: there are various
numbering ranges. This ranges are defined by a text code, a minimum and
maximum. Every bill have some code taken from the set defined in a
specific table (billnumberrange)
The first approach was the obvious "select into" a row instance, using
table%ROWTYPE as the destination.
That failed, leaving all fields of the instance as null.
But selecting into the interesting fields works ok. The trigger function
follows; the initial approach lines are commented.
CREATE FUNCTION nextbillnumber() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
lastnumber integer;
lowerlimit integer;
upperlimit integer;
-- ranger billnumberrange%ROWTYPE;
BEGIN
if NEW.billnumber = 0 THEN
select billnumberrange.lowerlimit, billnumberrange.upperlimit
from billnumberrange
where billnumberrange.groupcode = NEW.groupcode into
lowerlimit, upperlimit;
--where billnumberrange.groupcode = NEW.groupcode into ranger;
-- RAISE NOTICE 'first select result % % <> %',
ranger.groupcode, ranger.lowerlimit, ranger.upperlimit; this NOTICE
showed <NULL> <NULL> <> <NULL>
RAISE NOTICE 'first select result % <> %', lowerlimit,
upperlimit;-- this shows the expected values
select max(billnumber) from bill
where billnumber BETWEEN lowerlimit and upperlimit
-- where billnumber BETWEEN ranger.lowerlimit and
ranger.upperlimit
into lastnumber;
RAISE NOTICE 'second select result %', FOUND;
if lastnumber is null THEN
lastnumber := lowerlimit;
-- lastnumber := ranger.lowerlimit;
end if;
RAISE NOTICE 'lastnumber is %', lastnumber;
NEW.billnumber = lastnumber + 1;
end if;
return NEW;
END;
$$;
What was wrong in the first approach?
TIA
---
El software de antivirus Avast ha analizado este correo electrónico en busca de virus.
https://www.avast.com/antivirus
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Select into table%ROWTYPE failed
@ 2018-09-18 17:10 Tom Lane <tgl@sss.pgh.pa.us>
parent: marcelo <marcelo.nicolet@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Tom Lane @ 2018-09-18 17:10 UTC (permalink / raw)
To: marcelo <marcelo.nicolet@gmail.com>; +Cc: pgsql-general
marcelo <marcelo.nicolet@gmail.com> writes:
> What was wrong in the first approach?
plpgsql's "SELECT INTO" expects a one-for-one match between the output
columns of the SELECT and the columns of the INTO destination. So I'd
expect something like this to work:
DECLARE
ranger billnumberrange%ROWTYPE;
BEGIN
SELECT * FROM billnumberrange WHERE ... INTO ranger;
Your example clearly wasn't selecting all the columns, and it
wasn't clear whether you paid any attention to column ordering;
but both of those matter.
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Select into table%ROWTYPE failed
@ 2018-09-18 18:38 marcelo <marcelo.nicolet@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 3+ messages in thread
From: marcelo @ 2018-09-18 18:38 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general
Ohh, you are right!
Thank you!
On 18/09/2018 14:10 , Tom Lane wrote:
> marcelo <marcelo.nicolet@gmail.com> writes:
>> What was wrong in the first approach?
> plpgsql's "SELECT INTO" expects a one-for-one match between the output
> columns of the SELECT and the columns of the INTO destination. So I'd
> expect something like this to work:
>
> DECLARE
> ranger billnumberrange%ROWTYPE;
> BEGIN
> SELECT * FROM billnumberrange WHERE ... INTO ranger;
>
> Your example clearly wasn't selecting all the columns, and it
> wasn't clear whether you paid any attention to column ordering;
> but both of those matter.
>
> regards, tom lane
>
---
El software de antivirus Avast ha analizado este correo electrónico en busca de virus.
https://www.avast.com/antivirus
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2018-09-18 18:38 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-09-18 16:14 Select into table%ROWTYPE failed marcelo <marcelo.nicolet@gmail.com>
2018-09-18 17:10 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-09-18 18:38 ` marcelo <marcelo.nicolet@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