Re: PLPGSQL problem with SELECT INTO

From: "Jay O'Connor" <joconnor(at)cybermesa(dot)com>
To: "Jay O'Connor" <joconnor(at)cybermesa(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: PLPGSQL problem with SELECT INTO
Date: 2003-05-29 22:16:52
Message-ID: 20030529151652.C1144@altaica
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jim C. Nasby 2003-05-29 22:23:17 Re: Moving a table to a different schema
Previous Message nolan 2003-05-29 22:09:18 Re: How to deny user changing his own password?