重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 188|回复: 0
打印 上一主题 下一主题

[Oracle] oracle truncate 后数据找回

[复制链接]
跳转到指定楼层
楼主
发表于 2024-12-29 07:58:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
declare
  v_table_owner varchar2(32) default upper('&old_owner');
  v_table_name varchar2(32) default upper('&old_table_name');
  v_new_owner varchar2(32) default upper('&new_owner');
  v_new_table_name varchar2(32) default upper('&new_table_name');
   v_data_object_id number;
   v_tablespace_name varchar2(32);
   v_file_no number;
   v_start_block_no number;
   v_end_block_no number;
   v_rowid rowid;
   /*for client_info */
   v_free_blocks number;
   v_init_ext_blocks number;
   v_reused_blocks number;
   v_total_blocks number;
   v_scaned_blocks number default 0;
   v_rows_insert number default 0;
   /* end for client_info */
begin
  select data_object_id into v_data_object_id
  from dba_objects
  where object_type = 'TABLE'
  and owner = v_table_owner
  and object_name = v_table_name;
  select tablespace_name into v_tablespace_name
  from dba_tables
  where owner = v_table_owner
  and table_name = v_table_name;
    /*for client_info */
        dbms_application_info.set_client_info('caculating v_init_ext_blocks');
select blocks into v_init_ext_blocks
  from dba_extents
  where owner=v_table_owner
    and segment_name=v_table_name
    and extent_id=0 ;
          dbms_application_info.set_client_info('caculating v_free_blocks');
  select sum(blocks) into v_free_blocks
   from dba_free_space
   where tablespace_name = v_tablespace_name;
         dbms_application_info.set_client_info('caculating v_reused_blocks');
select sum(blocks) into  v_reused_blocks
              from (select relative_fno,block_id,blocks,row_number()over(partition by owner,segment_name,PARTITION_NAME order by extent_id desc) rn
                      from dba_extents
                     where tablespace_name = v_tablespace_name
                  )
              where rn=1   ;
   v_total_blocks := v_free_blocks + v_reused_blocks + v_init_ext_blocks;
    /*end for client_info */
   for i in (select relative_fno,block_id,blocks ,'INIT EXT' as segname
               from dba_extents
              where owner=v_table_owner
                and segment_name=v_table_name
                and extent_id=0
          union all
             select relative_fno,block_id,blocks ,'FREE' segname
               from dba_free_space
              where tablespace_name = v_tablespace_name
          union all
              select relative_fno,block_id,blocks ,segname
              from (select relative_fno,block_id,blocks,owner||'.'||segment_name segname, row_number()over(partition by owner,segment_name,PARTITION_NAME order by extent_id desc) rn
                      from dba_extents
                     where tablespace_name = v_tablespace_name
                  )
              where rn=1
    ) loop
   v_file_no:=i.relative_fno;
   v_start_block_no:=i.block_id;
   v_end_block_no:=i.block_id+i.blocks-1;   
   --for client info
   v_scaned_blocks:=v_scaned_blocks+i.blocks;
       for block# in v_start_block_no .. v_end_block_no loop
        begin
         for row# in 0 .. 999 loop
           v_rowid:=dbms_rowid.rowid_create(1, v_data_object_id,v_file_no,block#,row#);
           execute immediate 'insert into '||v_new_owner||'.'||v_table_name ||
                             ' select * from '||v_table_owner||'.'||v_table_name ||
                             ' where rowid = :rid ' using v_rowid;
   --for client info
           v_rows_insert:=v_rows_insert+sql%rowcount;
        dbms_application_info.set_client_info(
          'Scaned ' || round(v_scaned_blocks/v_total_blocks*100)||'%blocks(' || v_scaned_blocks ||'/'||v_total_blocks ||'), Inserted '|| v_rows_insert||' rows, current scaning '||i.segname);
         end loop;
        exception
          when others then
            null;
            --dbms_application_info.set_client_info(sqlerrm);
        end;
        commit;        
       end loop;
    end loop;
end;
/

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2025-2-5 02:41 , Processed in 0.090551 second(s), 22 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表