Re: SELECT in a function != SELECT ?

From: Jan Wieck <janwieck(at)yahoo(dot)com>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: Ian Morgan <imorgan(at)webcon(dot)net>, pgsql-sql(at)postgresql(dot)org
Subject: Re: SELECT in a function != SELECT ?
Date: 2002-04-25 13:53:19
Message-ID: 200204251353.g3PDrLd24680@saturn.janwieck.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Joe Conway wrote:
> Ian Morgan wrote:
> > CREATE FUNCTION get_colname (name,smallint)
> > RETURNS name AS '
> > SELECT a.attname FROM pg_class c, pg_attribute a
> > WHERE c.relname = ''$1''
> > and a.attnum = $2 and a.attrelid=c.oid
> > '
> > LANGUAGE SQL;
> >
> > SELECT get_colname('contacts',3);
> >
> > get_colname
> > -------------
> >
> > (1 row)
> >
>
> Try this:
>
> test=# CREATE FUNCTION get_colname (name,smallint) RETURNS name AS
> 'SELECT a.attname FROM pg_class c, pg_attribute a WHERE c.relname = $1
> and a.attnum = $2 and a.attrelid=c.oid' LANGUAGE SQL;
> CREATE
> test=# SELECT get_colname('foo',3);
> get_colname
> -------------
> f3
> (1 row)
>
> The $1 is already known to be a name, so you don't want the '' around it
> in the function definition.

Not because it's known to be a name, but because enclosing it
into quotes makes it the literal string '$1' instead of the
parameter passed in.

Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================== JanWieck(at)Yahoo(dot)com #

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2002-04-25 14:37:51 Re: Database Server in Recovery mode!
Previous Message Daniel Lundin 2002-04-25 13:08:38 Again: Identity not discovered by planner?