pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: Martín Marqués <martin@bugs.unl.edu.ar>
To: pgsql-sql@postgresql.org
Subject: Re: variables in procedures
Date: Fri, 30 Nov 2001 09:09:53 -0800
Message-ID: <web-517138@davinci.ethosmedia.com> (raw)
In-Reply-To: <20011130134344.BED7EFA61@bugs.unl.edu.ar>

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@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



view thread (3+ messages)  latest in thread

Message-ID: <web-517138@davinci.ethosmedia.com>
Permalink:  ../web-517138@davinci.ethosmedia.com/
Also on:    postgresql.org/message-id/web-517138@davinci.ethosmedia.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com, martin@bugs.unl.edu.ar
  Subject: Re: variables in procedures
  In-Reply-To: <web-517138@davinci.ethosmedia.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox