Re: PLPGSQL problem with SELECT INTO - Mailing list pgsql-general

From Jay O'Connor
Subject Re: PLPGSQL problem with SELECT INTO
Date
Msg-id 20030529151652.C1144@altaica
Whole thread Raw
In response to PLPGSQL problem with SELECT INTO  ("Jay O'Connor" <joconnor@cybermesa.com>)
List pgsql-general
> OK, I'm trying to count the number of records based on two criteria, this
> works at the psql prompt but not in a plpgsql function
>
>     SELECT count(*) FROM mytable WHERE fieldone = 'val1' AND fieldtwo
> =
> 'val2';
>
> This gives me back '4' which is what I expect (trust me :)
>
> but if I try to put this in a PLPGSQL function, it doesn't work.
>
>     CREATE FUNCTION countRows (varchar, varchar) RETURNS int AS
> '
>     DECLARE
>         val1 ALIAS FOR $1;
>         val2 ALIAS FOR $2;
>         total int;
>     BEGIN
>         SELECT INTO total count(*) FROM mytable WHERE fieldone =
> val1 AND fieldtwo = val2;
>         RETURN total;
>     END;
>     ' LANGUAGE PLPGSQL;


After some furtehr research I determined that it was ignoring the fieldTwo
check completely.  I finally narrowerd it ddown because my code read

    fieldTwo ALIAS FOR $2;

and the query read

    SELECT .....fieldtwo = fieldTwo

And plpgsql was not being case sensitive and thought it was comparing it to
itself.

Take care,
Jay


pgsql-general by date:

Previous
From: "scott.marlowe"
Date:
Subject: Re: Postmaster only takes 4-5% CPU
Next
From: lyris-pg@tibit.com
Date:
Subject: Re: fomatting an interval (resend)