pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
plpgsql: Bush becomes a null, can not execute null query
4+ messages / 4 participants
[nested] [flat]

* plpgsql: Bush becomes a null, can not execute null query
@ 2003-08-28 07:34 Mauri Sahlberg <Mauri.Sahlberg@claymountain.com>
  2003-08-28 13:33 ` Re: plpgsql: Bush becomes a null, can not execute null query Tom Lane <tgl@sss.pgh.pa.us>
  2003-08-28 17:55 ` Re: plpgsql: Bush becomes a null, can not execute null query Josh Goldberg <josh@4dmatrix.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Mauri Sahlberg @ 2003-08-28 07:34 UTC (permalink / raw)
  To: pgsql-admin

Hi,

This time I think this is a bug, but where? In my code or in plpgsql? Or
in my understanding of dynamic queries?

If the following is executed in context, where the dynamic query returns
data with every iteration everythin goes fine. If the the dynamic query
returns nothing - bush becomes null and everything fails. I do not
undestand why bush becomes null when the query returns nothing.

CREATE OR REPLACE FUNCTION 
  Paivat(varchar(9), date, date,varchar, integer)
RETURNS setof PS_TYOSUORITUKSET AS '
DECLARE
  aYritys ALIAS FOR $1;
  aPvm ALIAS FOR $2;
  alPvm ALIAS FOR $3;
  aKtunnus ALIAS FOR $4;
  aTunniste ALIAS FOR $5;

  rivi PS_TYOSUORITUKSET%ROWTYPE;
  latest INTEGER;
  nyt DATE;
  bush text;

BEGIN
  nyt:=aPvm;
  latest:=0;
  WHILE nyt <= alPvm LOOP
     bush:=''select * from PaivanLeimat('' ||
	quote_literal(aYritys) ||
	''::varchar,'' ||
        quote_literal(nyt) ||
        ''::date,''||
        quote_literal(aKtunnus) ||
        ''::varchar,'' ||
        latest
        ||'')'';
     RAISE NOTICE ''Query: %'', bush;
     FOR rivi IN EXECUTE bush LOOP
       RAISE NOTICE ''Ugh:%'',rivi.ytunnus;	
       RETURN NEXT rivi;
     END LOOP;
     latest:=rivi.suoritus;
     nyt:=nyt + interval ''1 day'';
     RAISE NOTICE ''%'', nyt;
   END LOOP;

   RETURN;
END;
' LANGUAGE 'plpgsql';

select * from  Paivat('0107262-0'::varchar, '2003-07-01'::date,
'2003-08-20'::date, 'colly_stig',0);

NOTICE:  Query: select * from
PaivanLeimat('0107262-0'::varchar,'2003-07-01'::date,'colly_stig'::varchar,0)
NOTICE:  Ugh:0107262-0
NOTICE:  Ugh:0107262-0
NOTICE:  Ugh:0107262-0
NOTICE:  2003-07-02
NOTICE:  Query: select * from
PaivanLeimat('0107262-0'::varchar,'2003-07-02'::date,'colly_stig'::varchar,26516)
NOTICE:  2003-07-03
NOTICE:  Query: <NULL>
WARNING:  Error occurred while executing PL/pgSQL function paivat
WARNING:  line 27 at for over execute statement





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: plpgsql: Bush becomes a null, can not execute null query
  2003-08-28 07:34 plpgsql: Bush becomes a null, can not execute null query Mauri Sahlberg <Mauri.Sahlberg@claymountain.com>
@ 2003-08-28 13:33 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 4+ messages in thread

From: Tom Lane @ 2003-08-28 13:33 UTC (permalink / raw)
  To: Mauri.Sahlberg@pretax.net; +Cc: pgsql-admin

Mauri Sahlberg <Mauri.Sahlberg@claymountain.com> writes:
> If the following is executed in context, where the dynamic query returns
> data with every iteration everythin goes fine. If the the dynamic query
> returns nothing - bush becomes null and everything fails. I do not
> undestand why bush becomes null when the query returns nothing.

If "latest" is null then the result of the series of concatenations is
also null.  Perhaps what you really want is to assign to "latest" inside
the inner FOR-loop.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: plpgsql: Bush becomes a null, can not execute null query
  2003-08-28 07:34 plpgsql: Bush becomes a null, can not execute null query Mauri Sahlberg <Mauri.Sahlberg@claymountain.com>
@ 2003-08-28 17:55 ` Josh Goldberg <josh@4dmatrix.com>
  2003-08-28 21:17   ` Re: plpgsql: Bush becomes a null, can not execute null query Oleg Bartunov <oleg@sai.msu.su>
  1 sibling, 1 reply; 4+ messages in thread

From: Josh Goldberg @ 2003-08-28 17:55 UTC (permalink / raw)
  To: Mauri.Sahlberg@pretax.net; +Cc: pgsql-admin

Concatenating a null value makes the string null.  try:
SELECT 'foo'||NULL||'bar';

Just add a condition to check rivi.suoritus for null before making an 
assignment.



Mauri Sahlberg wrote:

>returns nothing - bush becomes null and everything fails. I do not
>undestand why bush becomes null when the query returns nothing.
>  
>

>     bush:=''select * from PaivanLeimat('' ||
>	quote_literal(aYritys) ||
>	''::varchar,'' ||
>        quote_literal(nyt) ||
>        ''::date,''||
>        quote_literal(aKtunnus) ||
>        ''::varchar,'' ||
>        latest
>        ||'')'';
>  
>
<snip>

>     latest:=rivi.suoritus;
>  
>





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: plpgsql: Bush becomes a null, can not execute null query
  2003-08-28 07:34 plpgsql: Bush becomes a null, can not execute null query Mauri Sahlberg <Mauri.Sahlberg@claymountain.com>
  2003-08-28 17:55 ` Re: plpgsql: Bush becomes a null, can not execute null query Josh Goldberg <josh@4dmatrix.com>
@ 2003-08-28 21:17   ` Oleg Bartunov <oleg@sai.msu.su>
  0 siblings, 0 replies; 4+ messages in thread

From: Oleg Bartunov @ 2003-08-28 21:17 UTC (permalink / raw)
  To: Josh Goldberg <josh@4dmatrix.com>; +Cc: Mauri.Sahlberg@pretax.net; pgsql-admin

On Thu, 28 Aug 2003, Josh Goldberg wrote:

> Concatenating a null value makes the string null.  try:
> SELECT 'foo'||NULL||'bar';
>
> Just add a condition to check rivi.suoritus for null before making an
> assignment.

or better use coalesce

>
>
>
> Mauri Sahlberg wrote:
>
> >returns nothing - bush becomes null and everything fails. I do not
> >undestand why bush becomes null when the query returns nothing.
> >
> >
>
> >     bush:=''select * from PaivanLeimat('' ||
> >	quote_literal(aYritys) ||
> >	''::varchar,'' ||
> >        quote_literal(nyt) ||
> >        ''::date,''||
> >        quote_literal(aKtunnus) ||
> >        ''::varchar,'' ||
> >        latest
> >        ||'')'';
> >
> >
> <snip>
>
> >     latest:=rivi.suoritus;
> >
> >
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>

	Regards,
		Oleg
_____________________________________________________________
Oleg Bartunov, sci.researcher, hostmaster of AstroNet,
Sternberg Astronomical Institute, Moscow University (Russia)
Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(095)939-16-83, +007(095)939-23-83



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2003-08-28 21:17 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-08-28 07:34 plpgsql: Bush becomes a null, can not execute null query Mauri Sahlberg <Mauri.Sahlberg@claymountain.com>
2003-08-28 13:33 ` Tom Lane <tgl@sss.pgh.pa.us>
2003-08-28 17:55 ` Josh Goldberg <josh@4dmatrix.com>
2003-08-28 21:17   ` Oleg Bartunov <oleg@sai.msu.su>

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