Re: PLPGSQL Fetching rows

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Mark Nelson <mn(at)tardis(dot)cx>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: PLPGSQL Fetching rows
Date: 2003-05-20 20:44:26
Message-ID: 1724.1053463466@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Mark Nelson <mn(at)tardis(dot)cx> writes:
> CREATE FUNCTION UpdateNextProjectCode() RETURNS OPAQUE AS '
> DECLARE
> project_code INTEGER;
^^^^^^^^^^^^

> OPEN used_project_codes FOR SELECT project_code FROM projects WHERE
^^^^^^^^^^^^
> project_code > 0 ORDER BY project_code ASC;

It's a bad idea to use plpgsql variable names that match field or table
names that you are using in the same function. plpgsql generally
assumes that you want the variable, not the field or table. In this
case, what the SQL engine saw was effectively

SELECT NULL FROM projects WHERE NULL > 0 ORDER BY NULL ASC;

since the variable project_code contains NULL at the time the OPEN
executes.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Dmitri Bichko 2003-05-20 20:45:15 Re: Subqueries and the optimizer
Previous Message Network Administrator 2003-05-20 20:35:32 Fwd: Re: mod_perl + PostgreSQL implementation