PLPGSQL problem with SELECT INTO - Mailing list pgsql-general

From Jay O'Connor
Subject PLPGSQL problem with SELECT INTO
Date
Msg-id 20030529145321.A1144@altaica
Whole thread Raw
Responses Re: PLPGSQL problem with SELECT INTO
Re: PLPGSQL problem with SELECT INTO
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;


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

pgsql-general by date:

Previous
From: "scott.marlowe"
Date:
Subject: Re: FW: Blocking access to the database??
Next
From: Alvaro Herrera
Date:
Subject: Re: How to deny user changing his own password?