pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Table name as a variable in SELECT query
2+ messages / 2 participants
[nested] [flat]

* Table name as a variable in SELECT query
@ 2010-12-18 12:46 Andrey Fokin <affox@myway.com>
  2010-12-18 14:01 ` Re: Table name as a variable in SELECT query Andreas Kretschmer <akretschmer@spamfence.net>
  0 siblings, 1 reply; 2+ messages in thread

From: Andrey Fokin @ 2010-12-18 12:46 UTC (permalink / raw)
  To: pgsql-sql

Hi,
please advise is possible in SELECT query use variable as a table name?

I would like to have  for example something like:

CREATE OR REPLACE FUNCTION test_name (tb varchar) RETURNS int AS  $$

DECLARE 

                 ......

BEGIN
		SELECT * FROM $1;
		........

Will be grateful for any idea about.

 

Best regards
            Andrey  



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Table name as a variable in SELECT query
  2010-12-18 12:46 Table name as a variable in SELECT query Andrey Fokin <affox@myway.com>
@ 2010-12-18 14:01 ` Andreas Kretschmer <akretschmer@spamfence.net>
  0 siblings, 0 replies; 2+ messages in thread

From: Andreas Kretschmer @ 2010-12-18 14:01 UTC (permalink / raw)
  To: pgsql-sql

Andrey Fokin <affox@myway.com> wrote:

> Hi,
> please advise is possible in SELECT query use variable as a table name?
> 
> I would like to have  for example something like:
> 
> CREATE OR REPLACE FUNCTION test_name (tb varchar) RETURNS int AS  $$
> 
> DECLARE 
> 
>                  ......
> 
> BEGIN
> 		SELECT * FROM $1;
> 		........
> 
> Will be grateful for any idea about.

Yes, it is possible, with dynamic sql. You have to build a string that
contains your query, and then execute (it's a command in pl/pgsql) that
string. Read more:

http://www.postgresql.org/docs/current/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUT...


Andreas
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2010-12-18 14:01 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-12-18 12:46 Table name as a variable in SELECT query Andrey Fokin <affox@myway.com>
2010-12-18 14:01 ` Andreas Kretschmer <akretschmer@spamfence.net>

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