Skip to Main Content
  • Questions
  • Parameters as pl/sql table object to Parameterized View?

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Rafique.

Asked: July 24, 2004 - 12:54 pm UTC

Last updated: July 24, 2004 - 9:12 pm UTC

Version: 8.1.7

Viewed 1000+ times

You Asked

I am really happy that I get the chance again to ask you question.

Tom, I have requirement to access pl/sql table(table(cast) within definition of views as I want to push predicate into the view to increase performance as if I join view with pl/sql table on fly then performance degrades.

Currently I am using global variable within packages in order to implement this approach. Although this approach is working fine but still I am not satisfied with approach 100% because this involves using of global variables. Here are the sample examples of this approach.

create or replace type key_array_t as table of number;
/

create table t as select * from all_objects
/

create or replace view myview
as select * from t where object_id in(select column_value from table(cast(test.get_input_list()as key_array_t)))
/
create or replace package test as
type t_ref_cursor is ref cursor;
function get_input_list return key_Array_t;
procedure get_data(p_in_list key_Array_t,
P_CUR OUT t_ref_cursor);
end;
/

create or replace package body test as
g_input_list key_array_t;
function get_input_list return key_array_t is
begin
return g_input_list;
end;
procedure get_data(p_in_list key_Array_t,
P_CUR OUT t_ref_cursor) is
begin
g_input_list:=p_in_list;
open p_cur
for select * from myview;
end;
end;
/
var cur refcursor;
EXEC TEST.GET_DATA(KEY_ARRAY_T(100),:cur);
print :cur;
/

Is the above approach is the only solution or is there any other solution that more viable than above mentioned one?

BTW,I really want to avoid using of global variables in my application as they are always error prone.

Thanks in advance for your help.

and Tom said...

you can use an application context -- using str2tbl.

search for str2tbl -- you can populate a context with value,value,value,value -- it'll break it up and return it as a table type.


but in this case -- why a parameterized view at all? why not just embed the sql right in the stored procedure -- that is (in my opinion) the best way to have a parameterized view - a stored procedure that returns a result set!




Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks for your prompt response.

Rafique Awan, July 24, 2004 - 2:04 pm UTC

Thanks a lot for quick response.
First of all
I search this site for str2tbl but I did not find any useful example that I can implement according to my requirement without changing any code on Middle Tier side as right now we are populating pl/sql table on Java side and getting it as parameter to stored procedure. So I think in this case that to implement this approach involves lot of code changes on middle tier as well as on database.

secondly, Can you please provide me some examples that how this approach is better than approach I have right now?


Thirdly,

I agree with you that I can embed the definition of sql within stored procedure but

1) I am using this sql in more than one stored procedures as I embed sql within stored procedures, It will difficult to maintain in the long run.
2) By using view, I just have to maintain one view as If I will change the sql query , I just have to change it as one place, not in all stored procedures.

Thanks again.

Tom Kyte
July 24, 2004 - 9:12 pm UTC

but if you use this procedure from "everywhere" and not the view -- that won't be a problem...

it is not that this is "better", you asked for "different", this is "different".

the procedure that passes a "table" can just put the table into a string, put string into context and use str2tbl -- six one way, 1/2 dozen (which is just six) another. at the end of the day they should be equivalent but "different"

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library