Home » SQL & PL/SQL » SQL & PL/SQL » Create VIEW retrieving from dynamically configured table/queries
Create VIEW retrieving from dynamically configured table/queries [message #638430] Thu, 11 June 2015 09:13 Go to next message
gtriant
Messages: 42
Registered: September 2006
Member
Hello,

I am looking for the following.
To create a VIEW but to specify the source tables based on some parameter.
Something like:

CREATE OR REPLACE FORCE VIEW CCS3.NON_BILLABLE_ACCOUNTS2
(
   fname,
   lname
)
AS
   SELECT *
     FROM (CASE when get_flag('switch_table')=0
   (
   SELECT    fname,
                 lname
     FROM T1 t1, T2 t2
    WHERE t1.t1_id = t2.t2_id
    )

    else
    (
      SELECT    fname,
                    lname
     FROM P1 p1, P2 p2
    WHERE p1.p1_id = p2.p2_id
    )
);



Is this possible in general at all?

Thank you
Re: Create VIEW retrieving from dynamically configured table/queries [message #638431 is a reply to message #638430] Thu, 11 June 2015 09:33 Go to previous messageGo to next message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
What are you trying to achieve? It is not good design to create database objects on the fly.
Re: Create VIEW retrieving from dynamically configured table/queries [message #638432 is a reply to message #638431] Thu, 11 June 2015 09:57 Go to previous messageGo to next message
gtriant
Messages: 42
Registered: September 2006
Member
Hello

The starting point of the idea goes back a bit.
Upon another action/procedure which merges tables, until this big merge is done, some queries become very slow.
As one of the running ideas, we will create secondary table upon which the merge will occur on the secondary tables, while the others will serve the queries.
Re: Create VIEW retrieving from dynamically configured table/queries [message #638433 is a reply to message #638432] Thu, 11 June 2015 09:59 Go to previous messageGo to next message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
Have you considered Materialized Views?
Re: Create VIEW retrieving from dynamically configured table/queries [message #638434 is a reply to message #638430] Thu, 11 June 2015 10:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

It is not clear for me if you want to create a view with a definition depending on a parameter or if you want the view to select from one source (P1,P2) or the other one (T1,T2) depending on this parameter?

And what is the code of get_flag?

Re: Create VIEW retrieving from dynamically configured table/queries [message #638439 is a reply to message #638432] Thu, 11 June 2015 12:26 Go to previous messageGo to next message
gtriant
Messages: 42
Registered: September 2006
Member
Indeed this is also under consideration, separately from my main question
Re: Create VIEW retrieving from dynamically configured table/queries [message #638440 is a reply to message #638434] Thu, 11 June 2015 12:34 Go to previous messageGo to next message
gtriant
Messages: 42
Registered: September 2006
Member
Both are more or less correct.
The second is better phrasing of the idea.
The code of the dummy function is not significant. It would return a flag indicating which tables to use. This flag would be set/modified by the procedure that performs MERGE on the tables in question, so that upon merging the other set of tables would be used. Something like that.

If the idea is now more clear, would someone now if it is feasible?
Re: Create VIEW retrieving from dynamically configured table/queries [message #638441 is a reply to message #638440] Thu, 11 June 2015 12:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
This flag would be set/modified by the procedure that performs MERGE on the tables in question, so that upon merging the other set of tables would be used.


Do you mean a select on the view will modify the data in the tables it is currently querying?

So, yes, the code of the functional is not only significant but mandatory to give an accurate and appropriate answer.

[Updated on: Thu, 11 June 2015 12:43]

Report message to a moderator

Re: Create VIEW retrieving from dynamically configured table/queries [message #638442 is a reply to message #638430] Thu, 11 June 2015 13:10 Go to previous messageGo to next message
gtriant
Messages: 42
Registered: September 2006
Member
No, the flag is meant to be used upon the creation of the view (if this would be possible), as it is demonstrated in the code snippet.
I do not refer at all to the part where the view would be used. The function would not actually require any input. Think of it as a boolean parameter set externally.

Thanks!
Re: Create VIEW retrieving from dynamically configured table/queries [message #638443 is a reply to message #638442] Thu, 11 June 2015 13:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So this is the first part of my post "you want to create a view with a definition depending on a parameter" and not the second part "you want the view to select from one source (P1,P2) or the other one (T1,T2) depending on this parameter?" which is correct not the opposite as you said in your previous post.

It should be better if you post an example of what you want to do, no need to be the real tables, just create a representative example as for the moment I actually do not understand what you have and what you want to do.

Re: Create VIEW retrieving from dynamically configured table/queries [message #638444 is a reply to message #638442] Thu, 11 June 2015 13:24 Go to previous messageGo to next message
John Watson
Messages: 8929
Registered: January 2010
Location: Global Village
Senior Member
How about something like this,
create view twotab as
select ename from emp where get_flag=1
union all
select dname from dept where get_flag=0;
you'll see rows from each table depending on what your function returns.
Re: Create VIEW retrieving from dynamically configured table/queries [message #638558 is a reply to message #638444] Mon, 15 June 2015 06:23 Go to previous messageGo to next message
gtriant
Messages: 42
Registered: September 2006
Member
Hello,

Yes this is exactly the desired outcome.
This was the first approach for this solution, which had to be rejected, since regardless of filtering out the records correctly, the cost becomes double.
Thank you very much though.
Re: Create VIEW retrieving from dynamically configured table/queries [message #638559 is a reply to message #638558] Mon, 15 June 2015 06:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

No, the cost may not be double but it depends on the function, this is why I asked you so many times to post it.

Re: Create VIEW retrieving from dynamically configured table/queries [message #638568 is a reply to message #638559] Mon, 15 June 2015 08:21 Go to previous messageGo to next message
gtriant
Messages: 42
Registered: September 2006
Member
Hello,

The function that would check the flag indicating which set of tables to use is looking a name/value table called FLAGS, to retrieve the current value:

CREATE OR REPLACE FUNCTION "GET_FLAG" ( IN_FLAG_NAME IN FLAGS.NAME%TYPE ) RETURN FLAGS.VALUE%TYPE IS
 l_RESULT FLAGS.VALUE%TYPE;
BEGIN
  SELECT VALUE INTO l_RESULT FROM FLAGS WHERE NAME = IN_FLAG_NAME;

  RETURN l_RESULT;
END;
/


The value would be set by some other event not related to the main question.
Thank you.
Re: Create VIEW retrieving from dynamically configured table/queries [message #638574 is a reply to message #638568] Mon, 15 June 2015 09:05 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Why not just merge the function query into the view query and ditch the function?
Re: Create VIEW retrieving from dynamically configured table/queries [message #638575 is a reply to message #638568] Mon, 15 June 2015 09:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So it has a parameter when you said it has not.
What could be the distinct values for FLAGS.NAME?
What could be the distinct values for FLAGS.VALUE?

Quote:
The value would be set by some other event not related to the main question.


Is this a current session event or not?

Re: Create VIEW retrieving from dynamically configured table/queries [message #638587 is a reply to message #638574] Mon, 15 June 2015 10:44 Go to previous messageGo to next message
gtriant
Messages: 42
Registered: September 2006
Member
Well...

How would that go inside a create view?

Thanks
Re: Create VIEW retrieving from dynamically configured table/queries [message #638588 is a reply to message #638575] Mon, 15 June 2015 10:48 Go to previous messageGo to next message
gtriant
Messages: 42
Registered: September 2006
Member
Hello.

Parameter name would be "SWITCH_TABLE" always, parameter value would be "0" or "1" (or could be any two different values, even the table name to use).
The session changing these values as stated before is not the same.
I still don't see the significance of this, but not important.
Thank you very much.
Re: Create VIEW retrieving from dynamically configured table/queries [message #638589 is a reply to message #638588] Mon, 15 June 2015 11:00 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Explain how this would actually behave in a multi-user concurrent environment.
What happens when same schema USER in different sessions require different parameter values?
Re: Create VIEW retrieving from dynamically configured table/queries [message #638591 is a reply to message #638588] Mon, 15 June 2015 11:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

gtriant wrote on Mon, 15 June 2015 17:48
Hello.

Parameter name would be "SWITCH_TABLE" always, parameter value would be "0" or "1" (or could be any two different values, even the table name to use).
The session changing these values as stated before is not the same.
I still don't see the significance of this, but not important.
Thank you very much.


I advice you to create a global context and use a view similar to the one John posted:
create view twotab as
select ename from emp where sys_context('MyAppCtx','SWITCH_TABLE') != '0'
union all
select dname from dept where sys_context('MyAppCtx','SWITCH_TABLE') = '0';

You have to set the context at instance startup from the value inside the table.
When you modify the value in the table you also modify the value in the context.
A global context variable is a variable inside the SGA and so request no table access and cost almost nothing to get.
The view is defined with exclusive conditions, the optimizer knows it has to access one table or the other one but not both.


[Updated on: Wed, 17 June 2015 01:30]

Report message to a moderator

Re: Create VIEW retrieving from dynamically configured table/queries [message #638646 is a reply to message #638591] Tue, 16 June 2015 16:03 Go to previous message
gtriant
Messages: 42
Registered: September 2006
Member
Thank you I will give it a try
Previous Topic: How To Check Images with Zero Bytes in Size on BLOB column datatype
Next Topic: PL SQL - Need to create archive table - [MERGED]
Goto Forum:
  


Current Time: Fri Apr 19 05:39:25 CDT 2024