I am rather new to PL/pgSQL and don't know how to reference a variable in a SELECT
statement.
In this following function the SELECT INTO
always returns NULL
:
$body$
DECLARE
r RECORD;
c CURSOR FOR select name from t_people;
nb_bills integer;
BEGIN
OPEN c;
LOOP
FETCH c INTO r;
EXIT WHEN NOT FOUND;
RAISE NOTICE 'name found: %', r.name;
SELECT count(bill_id) INTO nb_bills from t_bills where name = r.name;
END LOOP;
END;
$body$
RAISE NOTICE
allows me to verify that my CURSOR
is working well: names are properly retrieved, but for some reason still unknown to me, not properly passed to the SELECT INTO
statement.
For debugging purpose, I tried to replace the variable in SELECT INTO
with a constant value and it worked:
SELECT count( bill_id) INTO nb_bills from t_bills where name = 'joe';
I don't know how to reference r.name
in the SELECT INTO
statement.
I tried r.name
, I tried to create another variable containing quotes, it is always returning NULL
.
I am stuck. If anyone knows ...