Oracle遊標-循環查詢表中數據(表名),並執行

Oralce 表中存有一個字段,該字段存儲表名,要把該表中的全部表名查詢出來(即表名結果集),且執行結果集from 表名結果集:oop

declare
  v_ccount varchar2(100);
  --定義一個遊標變量
  cursor c_job is
  --查詢該表中的全部表名
    select tablename from tbname;
  c_row c_job%rowtype;
begin
  --循環待處理數據,即以上查出的結果集
  for c_row in c_job loop
    ---執行語句 from 循環的表名   並插入
    execute immediate 'select count(1)  from ' || c_row.tablename
      into v_ccount;
    insert into tb_count
      (tbname2, ccount)
    values
      (c_row.tablename, v_ccount);
    --dbms_output.put_line(v_ccount);
  end loop; --循環結束
  commit;
end;
相關文章
相關標籤/搜索