PLPGSQL problem with SELECT INTO

Поиск
Список
Период
Сортировка
От Jay O'Connor
Тема PLPGSQL problem with SELECT INTO
Дата
Msg-id 20030529145321.A1144@altaica
обсуждение исходный текст
Ответы Re: PLPGSQL problem with SELECT INTO  ("Jay O'Connor" <joconnor@cybermesa.com>)
Re: PLPGSQL problem with SELECT INTO  (Tom Lane <tgl@sss.pgh.pa.us>)
Список 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 по дате отправления:

Предыдущее
От: "scott.marlowe"
Дата:
Сообщение: Re: FW: Blocking access to the database??
Следующее
От: Alvaro Herrera
Дата:
Сообщение: Re: How to deny user changing his own password?