PLPGSQL problem with SELECT INTO

From: "Jay O'Connor" <joconnor(at)cybermesa(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: PLPGSQL problem with SELECT INTO
Date: 2003-05-29 21:53:21
Message-ID: 20030529145321.A1144@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;

The value returned is much higher. Actaully, it is exactly what the number
should be without the AND query. No matter what I'. passing for the second
variable, I get the same result (even if it's a value not in that column
for any record)

Amy thoughts as to why this might be?

I'm using this in a procedure that will eventually delete certain rows from
a table and needs to decerement a field in another table based on how many
rows will be deleted? Can I just do a DELETE and use GET DIAGNOSTICS to
get the number of rows that were deleted?

Take care,
Jay

Responses

Browse pgsql-general by date

  From Date Subject
Next Message nolan 2003-05-29 22:09:18 Re: How to deny user changing his own password?
Previous Message Tom Lane 2003-05-29 21:41:29 Re: PLPGSQL problem with SELECT INTO