Returning 0 rows from a PL/PGSQL

From: Vitaly Belman <vitalyb(at)gmail(dot)com>
To: PostgreSQL general <pgsql-general(at)postgresql(dot)org>
Subject: Returning 0 rows from a PL/PGSQL
Date: 2005-02-19 23:59:33
Message-ID: fa96e3c605021915595ad1090c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have the following plpgsql function:

CREATE OR REPLACE FUNCTION public."temp"(int4)
RETURNS public.books AS
$BODY$DECLARE
old_book books%rowtype;
BEGIN
select * into old_book from books
where book_id = var_book_id;

IF FOUND = false THEN
return null;
ELSE
return old_book;
END IF;
END;$BODY$
LANGUAGE 'plpgsql' VOLATILE;

If the function finds a book with the given ID, it returns its row, if
it doesn't, it should return no rows at all (naturally it is
simplified version of what I need). In practice, however, it returns
either a regular row, or a regular row with all fields set to NULL.

So if in my PHP code I have:

$rs = pg_query("select * from temp(-60)");
pg_num_rows($rs);

It keeps printing 1 even if the ID I pass doesn't exist. What's my remedy?

Thanks

--
ICQ: 1912453
AIM: VitalyB1984
MSN: tmdagent(at)hotmail(dot)com
Yahoo!: VitalyBe

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Peter Eisentraut 2005-02-20 00:04:04 Re: quoting internal variable names
Previous Message Jonel Rienton 2005-02-19 23:51:53 Re: PGSQL 8.0.1 Win 2K Installation Problem