Hi,
you have basically 2 options.
1. Use dynamic query
2. Use collections
- With dynamic query you dynamically construct the query at runtime and than run it:
DECLARE
v_filename VARCHAR2(300) := '(''ALL_VIEWS'', ''ALL_TABLES'')';
--double quotes inside a string
cnt NUMBER;
BEGIN
execute immediate 'SELECT COUNT(*) INTO :cnt FROM all_views
WHERE view_name IN ' || v_filename
into cnt;
dbms_output.put_line('counted: ' || cnt);
END;
/
Advantages: easy to write, especially for short queries, fast
Disadvanteges: possible SQL INJECTION in cases when you wrongly paste user input in the query string, hard parse everytime you change the filename list
- Use collections. You create a collection type, then fill it and use it as a pseudo table.
create type tt_strings as table of varchar2(30);
declare
cnt NUMBER;
v_condition tt_strings;
begin
v_condition := tt_strings();
v_condition.extend;
v_condition(1) := 'ALL_VIEWS';
v_condition.extend;
v_condition(2) := 'ALL_TABLES';
SELECT COUNT(*)
INTO cnt
FROM all_views o, TABLE(v_condition) x
WHERE x.column_value = o.VIEW_NAME;
dbms_output.put_line('counted: ' || cnt);
end;
/
Advantages: Safe, maintainable for larger collections with more than just 2 elements
Disadvatages: You need to create a type, Harder to code (at first), a bit slower (which in 99% cases doesn't matter)