代碼如下:
declare s_sql clob:=''; -- 聲明一個變量,該變量用於存儲查詢的sql語句
要是在遍歷后的查詢語句中還要用模糊查詢這個與普通的sql語句也不同,直接代碼了:
declare s_sql clob:=''; -- 聲明一個變量,該變量用於存儲查詢的sql語句
v_cityCode varchar2(20); -- 每個sql語句執行完的查詢結果
code varchar2(20); -- 查詢條件
begin -- 開啟事務
code :='61';
for wf in(select table_name from user_tables where table_name like '__________________201710%') -- 從數據庫中的所有表中進行表名的模糊查詢, 然后依 次遍歷每個表名
loop -- 循環體
s_sql:= 'select count(*) from '|| wf.table_name||' where city_code like :code'; -- 組合查詢語句, 其中city_code like :code 的作用是使用code(下面的using code), 1 並沒有實際意義
execute immediate s_sql into v_cityCode using code||'%';--執行動態sql
dbms_output.put_line(' 事表'||wf.table_name||'有'||v_cityCode||'行西數據'); -- 輸出結果
end loop;
end;
oracle 遍歷數組並通過數組元素來創建表格
declare
i number;
begin
for i in 1..9 loop
begin
dbms_output.put_line(i); -- 輸出結果
exception
when others then
null;
end;
execute immediate 'create table CALYZES.CM_UNGCARD_CT_2015010'||i||' (
age_code varchar2(10) DEFAULT NULL,
)' ;
end loop;
end ;
commit;
補充:loop end loop 循環體內加if判斷可用 if 條件 then 執行語句 end if