Re: Selects in Function Return Wrong Value

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Nick Thuesen <jimmy(dot)mcshaw(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Selects in Function Return Wrong Value
Date: 2005-04-11 06:35:37
Message-ID: 22369.1113201337@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Nick Thuesen <jimmy(dot)mcshaw(at)gmail(dot)com> writes:
> CREATE OR REPLACE FUNCTION "public"."event_select_on_id" (event_id
^^^^^^^^
> integer) RETURNS "public"."event" AS
> $body$
> DECLARE
> return_event public.event;
> BEGIN
> SELECT * INTO return_event FROM event WHERE event_id = $1;
^^^^^^^^
> RETURN return_event;
> END;
> $body$
> LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

plpgsql is probably seeing this as "WHERE $1 = $1", ie, true at every
row of the table. It's a bad idea to name local variables or parameters
the same as table or field names that you need to reference in the
function, because plpgsql is very likely to misinterpret the field
references as variable references.

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message stig erikson 2005-04-11 07:34:58 Re: what is command to view data inside the table
Previous Message Bruce Momjian 2005-04-11 03:44:01 Re: ecpg for windows