Oracle sql execute sql from varchar field -


i have column in table sql-where clause stored:

  table_a   id  |  entry   --------------   1   |  "(select some_id table_one)"   -- stored varchar(4000)   2   |  "(select some_id table_one column_one 'test')" 

is possible use statement in entry column in select?

something like:

   select *    table_b, table_a     table_b.id = table_a.id      , table_b.value in --and here should entry column table_a 

you can't dynamicly add sql within sql.

alternatively can use encapsulate query logic in function , use dynamic sql in pl/sql.

for need create function (my_function in sql below) returns collection of string , accepts sql statement parameter , write query way

 select *    table_b, table_a     table_b.id = table_a.id      , table_b.value in (select column_value table(my_function(table_a.sql_statement)) 

performance not ignored approach. suggest evaluate consequence of context switching before going solution

additionally, you'll have analyze if sql injection possibility , make sure no malicious sql passed parameter function

sample code

create type varchar_tab_t table of varchar2(30); /   create or replace function my_function (sqlstring in varchar2) return varchar_tab_t  v_values_tab varchar_tab_t; begin    execute immediate sqlstring bulk collect v_values_tab;   return v_values_tab;   end my_function; /   table_a (id, sql_statement)    (select 1, 'select 1 dual union select 2 dual union select 3 dual' dual) , table_b (id, value)    (            select 1, 1 dual      union  select 1, 2 dual      union  select 1, 5 dual -- 1 should not shown    )    select *    table_b, table_a     table_b.id = table_a.id      , table_b.value in (select column_value table(my_function(table_a.sql_statement))) 

result

1   1   1   select 1 dual union select 2 dual union select 3 dual 1   2   1   select 1 dual union select 2 dual union select 3 dual 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -