Can you combine text variable together to referenece a VIEW name ?

Started by Bigjimabout 19 years ago4 messagesgeneral
Jump to latest
#1Bigjim
bigjim.still@rogers.com

Hi there,
I am trying to combine 2 text variable togther to form the name of a
VIEW. example

SELECT * FROM ( 'april'||'may') ;

I have a 12 different views and I want to be able to select a
different view depending on the contents of 2 fields in a database.

Has anyone done this before or have any ideas on how to do it ?

Thanks

#2Lew
lew@nospam.lewscanon.com
In reply to: Bigjim (#1)
Re: Can you combine text variable together to referenece a VIEW name ?

Bigjim wrote:

Hi there,
I am trying to combine 2 text variable togther to form the name of a
VIEW. example

SELECT * FROM ( 'april'||'may') ;

The table (or view) part is not a character string but an identifier.

--
Lew

In reply to: Bigjim (#1)
Re: Can you combine text variable together to referenece a VIEW name ?

On 09/04/2007 00:36, Bigjim wrote:

I am trying to combine 2 text variable togther to form the name of a
VIEW. example

SELECT * FROM ( 'april'||'may') ;

I think you want to use EXECUTE from within a pl/pgsql function to
construct and execute a query dynamically - have a look at this:

http://www.postgresql.org/docs/8.2/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

HTH,

Ray.

---------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod@iol.ie
---------------------------------------------------------------

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Raymond O'Donnell (#3)
Re: Can you combine text variable together to referenece a VIEW name ?

"Raymond O'Donnell" <rod@iol.ie> writes:

On 09/04/2007 00:36, Bigjim wrote:

I am trying to combine 2 text variable togther to form the name of a
VIEW. example
SELECT * FROM ( 'april'||'may') ;

I think you want to use EXECUTE from within a pl/pgsql function to
construct and execute a query dynamically - have a look at this:
http://www.postgresql.org/docs/8.2/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

That's the only way to do it (modulo that you can do this in any of the
PLs not only plpgsql) ... but a more general point is that the OP is
trying to swim against the tide. Almost certainly, rethinking the
design of those views is called for: try to merge them into one view.

regards, tom lane