Oracle 儲存過程中執行動態SQL,動態表名
阿新 • • 發佈:2019-02-04
create or replace procedure mw_sys.clearrubbishdatefy2 is type table_type is table of mw_app.mwt_ud_yscsjdl.tablename%type; tablenameArray table_type; str varchar(1000);--定義用於儲存動態SQL的變數 begin select tablename bulk collect into tablenameArray from mw_app.mwt_ud_yscsjdl group by tablename; for j in 1..tablenameArray.count loop---迴圈遍歷查詢表得到的結果 str :=' delete from '||'mw_app.'||tablenameArray(j)||' where obj_id in (select obj_id from mw_app.mwt_ud_yscsjdl where tablename = '''||tablenameArray(j)||''')' ;--拼接查詢SQL,單引號的轉義是通過雙引號進行的 execute immediate str;---立即執行拼接的動態SQL end loop; commit; --dbms_output.put('success'); dbms_output.put_line('success'); exception when others then --異常處理 rollback; -- dbms_output.put('error'); dbms_output.put_line('error'); end;