CSV -> table discussed in Tom's blog, etc
Duke Ganote, October 23, 2007 - 11:31 am UTC
Tom discusses this in several places, e.g.
http://tkyte.blogspot.com/2006/06/varying-in-lists.html
variable p_list varchar2(75)
/
exec :p_list := 'COMPANY,COMPANY_ADDRESS'
/
create or replace function foo
( p_str in varchar2
, p_delim in varchar2 default ','
)
return mytable as
l_str long default p_str || p_delim;
l_n number;
l_data mytable := mytable();
begin
loop
l_n := instr( l_str, p_delim );
exit when (nvl(l_n,0) = 0);
l_data.extend;
l_data(l_data.count) := ltrim(rtrim(substr(l_str,1,l_n-1)));
l_str := substr( l_str, l_n+1 );
end loop;
return l_data;
end;
/
select * from table(cast(foo(:p_list) as mytable))
/
select table_name
from user_tables
where table_name IN
( select * from table(cast(foo(:p_list) as mytable)) )
or table_name = case :p_list when 'ALL' then table_name end
/
The results:
SQL> select * from table(cast(foo(:p_list) as mytable));
COLUMN_VALUE
------------------------------
COMPANY
COMPANY_ADDRESS
SQL> select table_name
2 from user_tables
3 where table_name IN
4 ( select * from table(cast(foo(:p_list) as mytable)) )
5 or table_name = case :p_list when 'ALL' then table_name end
6 /
TABLE_NAME
------------------------------
COMPANY_ADDRESS
COMPANY
SQL> exec :p_list := 'ALL'
PL/SQL procedure successfully completed.
SQL> select table_name
2 from user_tables
3 where table_name IN
4 ( select * from table(cast(foo(:p_list) as mytable)) )
5 or table_name = case :p_list when 'ALL' then table_name end
6 /
TABLE_NAME
------------------------------
CO
TEST
T_COMPANY_ADDRESS
T_COMPANY
T_ADDRESS
ADDRESS
COMPANY_ADDRESS
COMPANY
COMPANY_STATE
T_COPY
TOM
PIECES
CO_ST
CERT_OF_AUTHORITY
14 rows selected.
(assuming you don't have a table named ALL).
October 23, 2007 - 1:27 pm UTC
ahh, thanks, i didn't read the full thing -
case instead of decode, valid.
However, your answer, more appropriate :)
thanks!
Excellent
Vinay Chandrakant, October 23, 2007 - 2:01 pm UTC
Thanks a ton, both! That was good!