你已经基本上2个选项。 1.使用动态查询 2.使用集合
- 使用动态查询动态地构造查询在运行时,比运行它:
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;
/
优点:容易写,尤其是对于短查询,快速 Disadvanteges:在您错误地在查询字符串中粘贴用户输入的情况下可能的SQL注入,每次更改文件名列表时都会进行硬解析
- 使用集合。您创建一个集合类型,然后填充它并将其用作伪表。
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;
/
优点:安全,维护与不止2个元素 Disadvatages大集合:你需要创建一个类型,更难代码(首先),有点慢(在99%的情况下,没关系)