Oracle中for循环查询多张表的记录数

oracle的表wjz_temp中有待查询的表名,通过for循环查询这些表的数据量,因为for循环不能用from 循环.表名,所以通过组装SQL,使用 execute immediate的形式实现。

例子如下

create or replace procedure pro_temp as

  v_cnt number;
  v_sql varchar2(100);
begin

  begin
    for i in (select w.*
                from wjz_temp w
               where w.segment_type in ('TABLE', 'TABLE PARTITION')
                 and w.num_rows is null
                 and w.segment_name not like '%LOG%'
                 and w.segment_name not like '%TMP%') loop
    
      v_sql := 'select count(0) from ' || i.segment_name;
    
      execute immediate v_sql  into v_cnt;
    
      dbms_output.put_line(i.segment_name || ':' || v_cnt);
    
      update wjz_temp w
         set w.num_rows = to_char(v_cnt)
       where w.segment_name = i.segment_name;
      commit;
    end loop;
  
  EXCEPTION
    WHEN others THEN
      dbms_output.put_line('SQLERRM:' || SQLERRM);
  end;

end pro_temp;

Oracle中如果想实现函数多个值的输出,在有多行的情况下肯定是不可以的;可以把多行转换为一行,可以通过cursor的方式实现,比如:

CURSOR attr IS

select name from wjz;

  FOR attrfor IN attr LOOP
        rreturnvalue := rreturnvalue||'---'||attrfor.name;
      END LOOP;

你可能感兴趣的:(oracle)