Re: Query with Parameters

From: Jeff Eckermann <jeff_eckermann(at)yahoo(dot)com>
To: Evil Azrael <evilazrael(at)evilazrael(dot)de>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Query with Parameters
Date: 2002-02-06 19:24:11
Message-ID: 20020206192411.12181.qmail@web20805.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

One possible way, from the psql command line:
\set field some_field
\set table some_table
\i myscript
where "myscript" contains:
select :field from :table;
You can do this from a script by calling psql with the
-f and -v options. Check the psql man page (or the
docs) for more information.

Alternatively, you could try a function: the field and
table names can be assigned dynamically using EXECUTE.
You could then populate a temporary table with the
results, and fetch those with a separate SELECT
statement (this gets around the inability to return
resultsets directly from the function).

--- Evil Azrael <evilazrael(at)evilazrael(dot)de> wrote:
> Hello!
>
> I hope this question was not asked before.
>
> What i need is SELECT with parameters. Afaik Views
> dont take
> parameters and functions cant return multiple
> tuples. Am i right? Or
> have i overseen something in the documentation or in
> the 1.8 million
> pages about PgSQL which Google.com knows?
>
> My main problem is that i have a complex query in
> which very often the
> same tables and fields are being used. A simple
> SQL-function would be
> very handy, but how do i return multiple records at
> the same time?
>
> as a simple example, is a function like this
> possible?
> SELECT funktion('field','table');
> where funktion is implemented like this :
> SELECT $1 FROM $2;
>
>
> Thanks for any answer in advance
>
> Christoph Nelles
> evilazrael(at)evilazrael(dot)de
>
>
> --
> Mit freundlichen Grssen
> Evil Azrael
> mailto:evilazrael(at)evilazrael(dot)de
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please
> send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org
> so that your
> message can get through to the mailing list cleanly

__________________________________________________
Do You Yahoo!?
Send FREE Valentine eCards with Yahoo! Greetings!
http://greetings.yahoo.com

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Marc G. Fournier 2002-02-06 20:47:45 JOIN between three *simple* tables ...
Previous Message Ian Barwick 2002-02-06 17:57:33 Re: Query with Parameters