oracle 語句之對數據庫的表名就行模糊查詢,對查詢結果進行遍歷,依次獲取每個表名結果中的每個字段(存儲過程)


語句的執行環境是plsql的sql窗口, 語句的目的是從整個數據庫中的所有表判斷 不等於某個字段的記錄數 。

代碼如下: 

  declare s_sql clob:=''; -- 聲明一個變量,該變量用於存儲查詢的sql語句

  v_cityCode varchar2(20); -- 每個sql語句執行完的查詢結果
  code varchar2(20); -- 查詢條件
  begin -- 開啟事務
    code :='110112'; 
    for wf in(select table_name from user_tables where table_name like '__________________201711%') -- 從數據庫中的所有表中進行表名的模糊查詢, 然后依  次遍歷每個表名
       loop -- 循環體
        s_sql:= 'select count(*) from '|| wf.table_name||' where city_code !=:1'; -- 組合查詢語句, 其中city_code != :1的作用是使用code(下面的using code), 1  並沒有實際意義
        execute immediate s_sql into v_cityCode using code;--執行動態sql 通過,using可以區分來添加多個變量,注意字段的大小寫的區分
       dbms_output.put_line(v_cityCode||' '||wf.table_name); -- 輸出結果
       end loop;
  end;

要是在遍歷后的查詢語句中還要用模糊查詢這個與普通的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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM