pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jan Wieck <janwieck@yahoo.com>
To: Joe Conway <mail@joeconway.com>
Cc: Ian Morgan <imorgan@webcon.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: SELECT in a function != SELECT ?
Date: Thu, 25 Apr 2002 09:53:19 -0400 (EDT)
Message-ID: <200204251353.g3PDrLd24680@saturn.janwieck.net> (raw)
In-Reply-To: <3CC79E91.5070903@joeconway.com>

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@Yahoo.com #





view thread (4+ messages)  latest in thread

Message-ID: <200204251353.g3PDrLd24680@saturn.janwieck.net>
Permalink:  ../200204251353.g3PDrLd24680@saturn.janwieck.net/
Also on:    postgresql.org/message-id/200204251353.g3PDrLd24680@saturn.janwieck.net

 · 

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: janwieck@yahoo.com, mail@joeconway.com, imorgan@webcon.net
  Subject: Re: SELECT in a function != SELECT ?
  In-Reply-To: <200204251353.g3PDrLd24680@saturn.janwieck.net>

* 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