Re: variables in procedures

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: Martín Marqués <martin(at)bugs(dot)unl(dot)edu(dot)ar>, pgsql-sql(at)postgresql(dot)org
Subject: Re: variables in procedures
Date: 2001-11-30 17:09:53
Message-ID: web-517138@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Martin,

> How can I distinguish between the name and the value of a variable?

You need to name your variables something *different* from your column
names. Otherwise, you will get unexpected errors.

A practice I usually follow is prefixing my column-substitiution
variables with "v_". This allows me, and the PL/pgSQL parser, to keep
the variables straight from the columns. I also need to fix your
procedure in a couple of places. Thus:

CREATE FUNCTION test(VARCHAR) RETURNS INTEGER AS '

DECLARE
v_col1 ALIAS FOR $1;
v_result INT;
BEGIN
select col1 INTO v_result
from tab where col1 = v_col1;
RETURN v_result;
END;
´ LANGUAGE 'plpgsql';

Other SQL-procedural languages (e.g. Transact-SQL) force you to prefix
your variables with special characters. In PL/pgSQL, it's up to you to
maintain consistency.

-Josh Berkus

______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Roberto Mello 2001-11-30 17:33:21 Re: Any available solution to port CONNECT BY of oracle to postgresql
Previous Message Randal L. Schwartz 2001-11-30 16:39:30 Re: contracting tables