你已經基本上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%的情況下,沒關係)