这两天都在研究怎么恢复被清空过的表,恢复的方法很多,可用的工具也很多,在这里分享一下个人认为比较方便的一种方法:
 
首先,我们分析一下TRUNCATE的过程。TRUNCATE不会逐个清除用户数据块上的数据,而仅仅重置数据字典和元数据块上的元数据(如存储段头和扩展段图)。也就是说,此时,其基本数据并未被破坏,而是被系统回收、等待被重新分配————因此,要恢复被TRUNCATE的数据,需要及时备份其所在的数据文件。
然后,再分析一下表扫描的过程:Oracle会读取段头的元数据,获得高水位线等信息,然后读取高水位线以下被格式化的数据块。因此,理论上讲,如果能够将被重置的元数据和元数据块重新构造出来,就能使数据能被重新读取。然而,要完成这个任务,难度相当大————要找出原有的所有元数据块被保证其每个字节与被TRUANCATE之前完全相同————看起来似乎是一个不可能完成的任务。
不过,我们可以换一角度来找方法————如果我们已经有一套元数据及数据块,然后将被TRUNCATE的用户数据块的内容取代其用户数据块的内容,是否可以“骗”过Oracle,让它读出这些数据呢?
回顾一下表扫描的过程,这个方法应该是可行的。我们只要想办法构造出一个结构相同、且具有完整元数据信息和格式化了的用户数据块的傀儡表对象,然后将被TRUNCATE的用户数据块找出,再将其数据内容部分嫁接到傀儡对象的用户数据块,使Oracle以外这是傀儡对象的数据,就能让Oracle扫描并读出数据内容。

恢复方法:新建两个表空间,分别存放A表和B表,表结构与源表一致。A表用于构造元数据,B表用于存放恢复数据。读取A表的元数据与源表的数据块,存放到新的数据文件。

调用语句:
declare  
    tgtowner varchar2(30);  
    tgttable varchar2(30);  
    datapath varchar2(4000);  
    datadir varchar2(30);  
    rects varchar2(30);  
    recfile varchar2(30);  
    rstts varchar2(30);  
    rstfile varchar2(30);  
    blksz number;  
    rectab varchar2(30);  
    rsttab varchar2(30);  
    copyfile varchar2(30);  
  begin  
    tgtowner := 'LAN'; --用户  
    tgttable := 'TEST1';  --要恢复的表名
    datapath := '/u01/app/oracle/oradata/db1/'; --数据文件所在的目录
    datadir := 'FY_DATA_DIR';   ---可保持不变
    pg_lan_recover_tab_data.prepare_files(tgtowner, tgttable, datapath, datadir, rects, recfile, rstts, rstfile, blksz);  
    pg_lan_recover_tab_data.fill_blocks(tgtowner, tgttable, datadir, rects, recfile, rstts, 8, tgtowner, tgtowner, rectab, rsttab, copyfile);  
    pg_lan_recover_tab_data.recover_table(tgtowner, tgttable, tgtowner, rectab, tgtowner, rsttab, datadir, datadir, recfile,datadir, copyfile, blksz);  
  end;

说明:如果存储用的是ASM,需将数据文件复制到数据库主机。 此代码可以恢复非分区表,可恢复普通压缩和非压缩表,如需恢复分区表与混合列压缩表,请与本人联系。

程序 代码:(需DBA与select any dictionary权限)
 create or replace package pg_lan_recover_tab_data authid current_user  is
  ---------------------------------------------------------------------------
  /* 需要权限:1、DBA角色;2、select and dictionary
 
  */
  ---------------------------------------------------------------------------
  type r_cursor is REF CURSOR;
  type o_fileprop is record (
   file# number,
   status$ number);
  type t_fileprops is table of o_fileprop;
  /************************************************************************
  ** Copy file
  **
  ** srcdir: Directory of Source File;
  ** srcfile: Source File Name;
  ** dstdir: Directory of Destination File;
  ** dstfile: Destination File Name;
  ************************************************************************/
  procedure copy_file(srcdir varchar2,
                      srcfile varchar2,
                      dstdir varchar2 default null,
                      dstfile varchar2 default null);
  /************************************************************************
  ** Recover Table Data From Special Data File;
  **
  ** oriobjid: Object Id of Table to be Recovered;
  ** recowner: Owner of Table to be used as recovering dummy table;
  ** rectable: Name of Table to be used as recovering dummy table;
  ** rstowner: Owner of Table to store the recovered data;
  ** rsttable: Name of Table to store the recovered data;
  ** srcdir: Directory of the Data File to be recovered;
  ** srcfile: Name of the Data File to be recovered;
  ** recdir: Directory of Data File that rectable is stored;
  ** recfile: Name of Data File that rectable is stored;
  ** copydir: Directory of Copy of Data File that rectable is stored;
  ** coryfile: Name of Copy of Data File that rectable is stored;
  ** blksz: Block size of the Tablespace Storing the Table to be recovered;
  ** selflink: database link refer to instance self connect to dba account;
  ************************************************************************/
  procedure recover_table(oriobjid number,
                          recowner varchar2,
                          rectab varchar2,
                          rstowner varchar2,
                          rsttab varchar2,
                          srcdir varchar2,
                          srcfile varchar2,
                          recdir varchar2,
                          recfile varchar2,
                          copydir varchar2,
                          copyfile varchar2,
                          blksz number default 8192,
                          fillblks number default 5,
                          selflink varchar2 default '',
                          endianess number default 1);
  /************************************************************************
  ** Recover Table Data From Data Files of Targe Table;
  **
  ** tgtowner: Owner of Target Table to be recovered;
  ** tgttable: Name of Target Table to be recovered;
  ** recowner: Owner of Table to be used as recovering dummy table;
  ** rectable: Name of Table to be used as recovering dummy table;
  ** rstowner: Owner of Table to store the recovered data;
  ** rsttable: Name of Table to store the recovered data;
  ** srcdir: Directory of the Data File to be recovered;
  ** srcfile: Name of the Data File to be recovered;
  ** recdir: Directory of Data File that rectable is stored;
  ** recfile: Name of Data File that rectable is stored;
  ** copydir: Directory of Copy of Data File that rectable is stored;
  ** coryfile: Name of Copy of Data File that rectable is stored;
  ** blksz: Block size of the Tablespace Storing the Table to be recovered;
  ** selflink: database link refer to instance self connect to dba account;
  ************************************************************************/
  procedure recover_table(tgtowner varchar2,
                          tgttable varchar2,
                          recowner varchar2,
                          rectab varchar2,
                          rstowner varchar2,
                          rsttab varchar2,
                          srcdir varchar2,
                          recdir varchar2,
                          recfile varchar2,
                          copydir varchar2,
                          copyfile varchar2,
                          blksz number default 8192,
                          fillblks number default 5,
                          selflink varchar2 default '');
  /************************************************************************
  ** Prepare the data files to be use during recovering;
  **
  ** tgtowner: Owner of Target Table to be recovered;
  ** tgttable: Name of Target Table to be recovered;
  ** datapath: Absolute path of Data Files;
  ** datadir: Directory to be created referring to datapath;
  ** rects: Tablespace to store the recovering dummy table;
  ** recfile: Name of Data File to store the recovering dummy table;
  ** rstts: Tablespace to store table storing the recovered data;
  ** rstfile: Name of Data File to store restoring table;
  ** blksz: Block size of the Tablespace Storing the Table to be recovered;
  ** rectsblks: block number of recovery tablespace
  ** rectsblks: block number of restore tablespace
  ************************************************************************/
  procedure prepare_files(tgtowner varchar2,
                          tgttable varchar2,
                          datapath in varchar2,
                          datadir in out varchar2,
                          rects out varchar2,
                          recfile out varchar2,
                          rstts out varchar2,
                          rstfile out varchar2,
                          blksz out varchar2,
                          rectsblks number default 16,
                          rsttsblks number default 2560);
  /************************************************************************
  ** Clean up existing Recover and Restore Tablespace. Drop tables in the tablespaces
  **
  ** rects: Recover tablespace name
  ** rects: Restore tablespace name, default NULL, will not do cleaning up;
  ************************************************************************/
  procedure clean_up_ts(rects varchar2,
                        rstts varchar2 default null);
  /************************************************************************
  ** Fill Blocks of Recovering Table, to format the blocks;
  **
  ** tgtowner: Owner of Target Table to be recovered;
  ** tgttable: Name of Target Table to be recovered;
  ** datadir: Directory to be created referring to datapath;
  ** rects: Tablespace to store the recovering dummy table;
  ** recfile: Name of Data File to store the recovering dummy table;
  ** rstts: Tablespace to store table storing the recovered data;
  ** blks: Number blocks in Initial Extent of the recovering dummy table;
  ** recowner: Owner of Table to be used as recovering dummy table;
  ** rstowner: Owner of Table to store the recovered data;
  ** rectable: Name of Table to be used as recovering dummy table;
  ** rsttable: Name of Table to store the recovered data;
  ** coryfile: Name of Copy of Data File that rectable is stored;
  ************************************************************************/
  procedure fill_blocks(tgtowner varchar2,
                        tgttable varchar2,
                        datadir varchar2,
                        rects varchar2,
                        recfile varchar2,
                        rstts varchar2,
                        blks number default 8,
                        recowner varchar2 default user,
                        rstowner varchar2 default user,
                        rectab in out varchar2,
                        rsttab in out varchar2,
                        copyfile out varchar2);
  /************************************************************************
  ** Testing recovering procedure, involved initializing tablespaces and data files
  **
  ** tgtowner: Owner of Target Table to be recovered;
  ** tgttable: Name of Target Table to be recovered;
  ** datapath: Absolute path of Data Files;
  ** fbks: block number to be filled in recovery table;
  ************************************************************************/
  procedure test_rec1( tow varchar2 default 'SYS',
                       ttb varchar2 default 'T_CHAIN',
                       fbks number default 1,
                       datapath varchar2 default 'D:\oracle\product\10.2.0\oradata\EDGAR\DATAFILE\');
  /************************************************************************
  ** Testing recovering procedure, without initializing tablespaces and data files
  **
  ** tgtowner: Owner of Target Table to be recovered;
  ** tgttable: Name of Target Table to be recovered;
  ** fbks: block number to be filled in recovery table;
  ************************************************************************/
  procedure test_rec2( tow varchar2 default 'SYS',
                       ttb varchar2 default 'T_CHAIN',
                       fbks number default 1);
  /************************************************************************
  ** Set Initial parameters
  **
  ** tracing: trace the process for debug;
  ** logging: show logging information;
  ** repobjid: replace the data object id wiht the recover table data object id;
  ************************************************************************/
  procedure init_set( tracing boolean default true,
                      logging boolean default true,
                      repobjid boolean default true);
end pg_lan_recover_tab_data;
/
create or replace package body pg_lan_recover_tab_data is
  ---------------------------------------------------------------------------
 
  ---------------------------------------------------------------------------
  s_tracing       boolean:= true;
  s_logging       boolean:= true;
  s_repobjid      boolean:= false;
  procedure init_set (tracing boolean default true,
                      logging boolean default true,
                      repobjid boolean default true)
  as
  begin
    s_tracing := tracing;
    s_logging := logging;
    s_repobjid := repobjid;
  end;
  procedure trace (msg varchar2)
  as
  begin
    if s_tracing then
      dbms_output.put_line(to_char(sysdate, 'HH24:MI:SS')||': '||msg);
    end if;
  end;
  procedure log (msg varchar2)
  as
  begin
    if s_logging then
      dbms_output.put_line(to_char(sysdate, 'HH24:MI:SS')||': '||msg);
    end if;
  end;
  function d2r (dig varchar2,
                len number default 0)
  return raw
  is
  begin
    --trace('[d2r] hextoraw(lpad(trim(to_char('||dig||', ''XXXXXXXX'')),'||len||',''0''))');
    return hextoraw(lpad(trim(to_char(dig, 'XXXXXXXX')),len,'0'));
  end;
  procedure copy_file(srcdir varchar2,
                      srcfile varchar2,
                      dstdir varchar2 default null,
                      dstfile varchar2 default null)
  as
    bfr    utl_file.file_type;
    bfw    utl_file.file_type;
    -- vrw    raw(8192);
    frw    raw(8192);
    bsz    number := 8192;
    --p_srcdir varchar2(255) := upper(srcdir);
    --p_srcfile varchar2(255) := upper(srcfile);
    --p_dstdir varchar2(255) := upper(dstdir);
    --p_dstfile varchar2(255) := upper(dstfile);
    p_srcdir varchar2(255) := srcdir;
    p_srcfile varchar2(255) := srcfile;
    p_dstdir varchar2(255) := dstdir;
    p_dstfile varchar2(255) := dstfile;
  begin
    if dstdir is null then
      p_dstdir := p_srcdir;
    end if;
    if p_dstfile is null then
      p_dstfile := p_srcfile||'$';
    end if;
    trace('[copy_file] begin copy file: '||srcdir||'\'||srcfile||' => '||dstdir||'\'||dstfile);
    bfr := utl_file.fopen(p_srcdir, p_srcfile, 'RB');
    bfw := utl_file.fopen(p_dstdir, p_dstfile, 'WB');
    while true loop
    begin
      utl_file.get_raw(bfr, frw, bsz);
      exit when frw is null;
      utl_file.put_raw(bfw, frw);
      utl_file.fflush(bfw);
      exception when others then
        exit;
    end;
    end loop;
    utl_file.fclose(bfw);
    utl_file.fclose(bfr);
    trace('[copy_file] completed.');
  end;
  procedure replace_segmeta_in_file(srcdir varchar2,
                                    srcfile varchar2,
                                    dstdir varchar2,
                                    dstfile varchar2,
                                    tgtobjid number,
                                    newobjid number,
                                    dtail raw,
                                    addpos number,
                                    addinfo raw,
                                    blksz number default 8192,
                                    endianess number default 1)
  as
    bfr    utl_file.file_type;
    bfw    utl_file.file_type;
    hsz    number := 24;
    objr   raw(4);
    objn   number;
    dhead  raw(32);
    dbody  raw(32767);
    nbody  raw(32767);
    p_srcdir varchar2(255) := srcdir;
    p_srcfile varchar2(255) := srcfile;
    p_dstdir varchar2(255) := dstdir;
    p_dstfile varchar2(255) := dstfile;
  begin
    if p_dstdir is null then
      p_dstdir := p_srcdir;
    end if;
    trace('[replace_objid_in_file] replace object id in '||srcdir||'\'||srcfile||' ['||tgtobjid||' => '||newobjid||']');
    bfr := utl_file.fopen(p_srcdir, p_srcfile, 'RB');
    bfw := utl_file.fopen(p_dstdir, p_dstfile, 'WB');
    while true loop
    begin
      nbody := '';
      utl_file.get_raw(bfr, dhead, hsz);
      exit when dhead is null;
      utl_file.get_raw(bfr, dbody, blksz-hsz);
      --objr := hextoraw(substrb(rawtohex(dbody), 1, 8));
      objr := utl_raw.substr(dbody, 1, 4);
      if endianess > 0 then
        objn := to_number(rawtohex(utl_raw.reverse(objr)), 'XXXXXXXX');
      else
        objn := to_number(rawtohex(objr), 'XXXXXXXX');
      end if;
      -- replace data object id with the recover object id
      --if objn = tgtobjid and substrb(rawtohex(dhead), 1, 2) = '06' then
      if objn = tgtobjid then
        if addpos <= hsz then
          --utl_file.put_raw(bfw, utl_raw.concat(utl_raw.substr(dhead, 1, addpos-1), addinfo, utl_raw.substr(dhead, addpos+utl_raw.length(addinfo))));
          nbody := utl_raw.concat(nbody, utl_raw.substr(dhead, 1, addpos-1), addinfo, utl_raw.substr(dhead, addpos+utl_raw.length(addinfo)));
        else
          --utl_file.put_raw(bfw, dhead);
          nbody := utl_raw.concat(nbody, dhead);
        end if;
        --utl_file.put_raw(bfw, utl_raw.concat(utl_raw.substr(dhead, 1, 8), addinfo, utl_raw.substr(dhead, 9+utl_raw.length(addinfo))));
        --nbody := utl_raw.concat(nbody, utl_raw.substr(dhead, 1, 8), addinfo, utl_raw.substr(dhead, 9+utl_raw.length(addinfo)));
        --trace('[replace_objid_in_file] old id in raw: '||rawtohex(objr));
        if endianess > 0 then
          --trace('[replace_objid_in_file] new id in raw: '||utl_raw.reverse(d2r(newobjid, 8)));
          --utl_file.put_raw(bfw, utl_raw.reverse(d2r(newobjid, 8)));
          nbody := utl_raw.concat(nbody, utl_raw.reverse(d2r(newobjid, 8)));
        else
          --trace('[replace_objid_in_file] new id in raw: '||(d2r(newobjid, 8)));
          --utl_file.put_raw(bfw, d2r(newobjid, 8));
          nbody := utl_raw.concat(nbody, d2r(newobjid, 8));
        end if;
        -- skip objid
        if addpos > hsz+5 and addinfo is not null then
          trace('[replace_objid_in_file] old body len: '||utl_raw.length(dbody)||' new = 4 + '||utl_raw.length(utl_raw.substr(dbody, 5, addpos-hsz-5))||' + '||utl_raw.length(addinfo)||' + '||utl_raw.length(utl_raw.substr(dbody, addpos-hsz-4+utl_raw.length(addinfo), blksz-(addpos-1)-utl_raw.length(dtail)-utl_raw.length(addinfo)))||' + 4');
          --utl_file.put_raw(bfw, utl_raw.concat(utl_raw.substr(dbody, 5, addpos-hsz-5), addinfo, utl_raw.substr(dbody, addpos-hsz, blksz-(addpos-1)-utl_raw.length(dtail)-utl_raw.length(addinfo))));
          nbody := utl_raw.concat(nbody, utl_raw.substr(dbody, 5, addpos-hsz-5), addinfo, utl_raw.substr(dbody, addpos-hsz-4+utl_raw.length(addinfo), blksz-(addpos-1)-utl_raw.length(dtail)-utl_raw.length(addinfo)));
          --trace('[replace_objid_in_file] new body len: '||utl_raw.length(nbody));
        elsif addpos = hsz+5 and addinfo is not null then
          --utl_file.put_raw(bfw, utl_raw.concat(addinfo, utl_raw.substr(dbody, addpos-hsz, blksz-(addpos-1)-utl_raw.length(dtail)-utl_raw.length(addinfo))));
          nbody := utl_raw.concat(nbody, addinfo, utl_raw.substr(dbody, addpos-hsz, blksz-(addpos-1)-utl_raw.length(dtail)-utl_raw.length(addinfo)));
        else
          --utl_file.put_raw(bfw, utl_raw.substr(dbody, 5, blksz-hsz-4-utl_raw.length(dtail)));
          nbody := utl_raw.concat(nbody, utl_raw.substr(dbody, 5, blksz-hsz-4-utl_raw.length(dtail)));
        end if;
        --trace('[replace_objid_in_file] tail in raw: '||dtail||'('||utl_raw.length(dtail)||')');
        --utl_file.put_raw(bfw, dtail);
        nbody := utl_raw.concat(nbody, dtail);
        trace('[replace_objid_in_file] new body length: '||utl_raw.length(nbody));
      else
        --utl_file.put_raw(bfw, dhead);
        --utl_file.put_raw(bfw, dbody);
        nbody := utl_raw.concat(nbody, dhead, dbody);
      end if;
      --if utl_raw.length(nbody) != blksz then
      --  trace('[replace_objid_in_file] new body length: '||utl_raw.length(nbody));
      --end if;
      utl_file.put_raw(bfw, nbody);
      utl_file.fflush(bfw);
      exception
        when no_data_found then
          exit;
        when others then
          trace('[replace_objid_in_file] '||SQLERRM);
          trace('[replace_objid_in_file] '||dbms_utility.format_error_backtrace);
          exit;
    end;
    end loop;
    utl_file.fclose(bfw);
    utl_file.fclose(bfr);
    trace('[replace_objid_in_file] completed.');
  end;
  function gen_table_name(tgttable varchar2,
                          plus     varchar2 default '',
                          genowner varchar2 default user)
  return varchar2
  as
    gentab varchar2(30);
  begin
    select upper(tgttable||plus||surfix) into gentab from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_tables where owner = genowner and table_name = upper(tgttable||plus||surfix)) order by surfix nulls first) where rownum<=1;
    return gentab;
  end;
  function gen_file_name( tgtfile varchar2,
                          plus    varchar2 default '')
  return varchar2
  as
    genfile varchar2(30);
  begin
    select tgtfile||plus||surfix||'.DAT' into genfile from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_data_files where file_name like '%\'||tgtfile||plus||surfix||'.DAT') order by surfix nulls first) where rownum<=1;
    return genfile;
  end;
  function gen_ts_name( tgtts  varchar2,
                        plus   varchar2 default '')
  return varchar2
  as
    gents varchar2(30);
  begin
    select tgtts||plus||surfix into gents from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_tablespaces where tablespace_name = tgtts||plus||surfix) order by surfix nulls first) where rownum<=1;
    return gents;
  end;
  function get_cols_no_lob( recowner varchar2,
                            rectab varchar2)
  return varchar2
  as
    cols        varchar2(32767);
    colno       number := 0;
  begin
    cols := '';
    for col_rec in (select column_name, data_type, nullable from dba_tab_cols where owner = recowner and table_name = rectab) loop
      if col_rec.data_type NOT LIKE '%LOB' then
        if colno > 0 then
          cols := cols||',';
        end if;
        cols := cols||col_rec.column_name;
        colno := colno + 1;
      end if;
    end loop;
    return cols;
  end;
  function restore_table_row_no_lob(recowner varchar2,
                                    rectab varchar2,
                                    rstowner varchar2,
                                    rsttab varchar2,
                                    cols varchar2,
                                    rid rowid)
  return number
  as
    recnum      number := 0;
  begin
    begin
      execute immediate 'insert /*+*/ into '||rstowner||'.'||rsttab||'('||cols||') select '||cols||' from '||recowner||'.'||rectab||' where rowid = :rid' using rid;
      recnum := recnum + SQL%ROWCOUNT;
    exception when others then
      trace('[restore_table_row_no_lob] '||SQLERRM);
      trace('[restore_table_row_no_lob] '||dbms_utility.format_error_backtrace);
      null;
    end;
    return recnum;
  end;
  function restore_table_in_rows( recowner varchar2,
                                  rectab varchar2,
                                  rstowner varchar2,
                                  rsttab varchar2)
  return number
  as
    recnum      number := 0;
    blk_cur     r_cursor;
    objid       number;
    fid         number;
    blkno       number;
    rnum        number;
    gnum        number;
    cols        varchar2(32767);
  begin
    begin
      --trace('[restore_table_in_rows] '||'select dbms_rowid.rowid_object(rowid) objid, dbms_rowid.rowid_relative_fno(rowid) fid, dbms_rowid.rowid_block_number(rowid) blkno, count(1) rnum from '||recowner||'.'||rectab||' group by dbms_rowid.rowid_object(rowid), dbms_rowid.rowid_relative_fno(rowid), dbms_rowid.rowid_block_number(rowid)');
      open blk_cur for 'select dbms_rowid.rowid_object(rowid) objid, dbms_rowid.rowid_relative_fno(rowid) fid, dbms_rowid.rowid_block_number(rowid) blkno, count(1) rnum from '||recowner||'.'||rectab||' group by dbms_rowid.rowid_object(rowid), dbms_rowid.rowid_relative_fno(rowid), dbms_rowid.rowid_block_number(rowid)';
      loop
        fetch blk_cur into objid, fid, blkno, rnum;
        exit when blk_cur%NOTFOUND;
        trace('[restore_table_in_rows] expected rows: '||rnum);
        gnum := 0;
        --trace('[restore_table_in_rows] block: '||blkno);
        for i in 1..rnum loop
        begin
          --trace('[restore_table_in_rows] row: '||i);
          --execute immediate 'insert /*+*/into '||rstowner||'.'||rsttab||' select * from '||recowner||'.'||rectab||' where rowid = dbms_rowid.rowid_create(1, :objid, :fid, :blkno, :i)' using objid, fid, blkno, i-1;
          execute immediate 'insert /*+*/into '||rstowner||'.'||rsttab||' select * from '||recowner||'.'||rectab||' where dbms_rowid.rowid_relative_fno(ROWID)=:fid and dbms_rowid.rowid_block_number(ROWID)=:blkno and dbms_rowid.rowid_row_number(ROWID)=:i' using fid, blkno, i-1;
          recnum := recnum + SQL%ROWCOUNT;
          gnum := gnum + SQL%ROWCOUNT;
        exception when others then
          if sqlcode = -22922 then
            -- trace('[restore_table_in_rows] Warning: Unrecoverable Lob found!');
            if cols is null then
              cols := get_cols_no_lob(recowner, rectab);
            end if;
            recnum := recnum + restore_table_row_no_lob(recowner, rectab, rstowner, rsttab, cols, dbms_rowid.rowid_create(1, objid, fid, blkno, i-1));
          else
            trace('[restore_table_in_rows] '||SQLERRM);
            trace('[restore_table_in_rows] '||dbms_utility.format_error_backtrace);
          end if;
          null;
        end;
        end loop;
        if gnum != rnum then
          log('Warning: '||(rnum-gnum)||' records lost!');
        end if;
      end loop;
      exception when others then
        trace('[restore_table_in_rows] '||sqlerrm);
        trace('[restore_table_in_rows] '||dbms_utility.format_error_backtrace);
        null;
    end;
    return recnum;
  end;
  function restore_table_in_rows_remote(recowner varchar2,
                                        rectab varchar2,
                                        rstowner varchar2,
                                        rsttab varchar2,
                                        dblink varchar2)
  return number
  as
    recnum      number := 0;
    blk_cur     r_cursor;
    objid       number;
    fid         number;
    blkno       number;
    rnum        number;
    cols        varchar2(32767);
  begin
    begin
      --rollback;
      open blk_cur for 'select dbms_rowid.rowid_object(rowid) objid, dbms_rowid.rowid_relative_fno(rowid) fid, dbms_rowid.rowid_block_number(rowid) blkno, count(1) rnum from '||recowner||'.'||rectab||' group by dbms_rowid.rowid_object(rowid), dbms_rowid.rowid_relative_fno(rowid), dbms_rowid.rowid_block_number(rowid)';
      loop
        fetch blk_cur into objid, fid, blkno, rnum;
        exit when blk_cur%NOTFOUND;
        trace('[restore_table_in_rows_remote] expected rows: '||rnum);
        for i in 1..rnum loop
        begin
          --execute immediate 'insert /*+no_append*/into '||rstowner||'.'||rsttab||' select * from '||recowner||'.'||rectab||'@'||dblink||' where rowid = dbms_rowid.rowid_create(1, :objid, :fid, :blkno, :i)' using objid, fid, blkno, i-1;
          execute immediate 'insert /*+*/into '||rstowner||'.'||rsttab||' select * from '||recowner||'.'||rectab||'@'||dblink||' where dbms_rowid.rowid_relative_fno(ROWID)=:fid and dbms_rowid.rowid_block_number(ROWID)=:blkno and dbms_rowid.rowid_row_number(ROWID)=:i' using fid, blkno, i-1;
          recnum := recnum + SQL%ROWCOUNT;
          --commit;
        exception when others then
          if sqlcode = -22922 then
            if cols is null then
              cols := get_cols_no_lob(recowner, rectab);
            end if;
            recnum := recnum + restore_table_row_no_lob(recowner, rectab, rstowner, rsttab, cols, dbms_rowid.rowid_create(1, objid, fid, blkno, i-1));
          else
            trace('[restore_table_in_rows_remote] '||SQLERRM);
            trace('[restore_table_in_rows_remote] '||dbms_utility.format_error_backtrace);
            --commit;
          end if;
          null;
        end;
        end loop;
      end loop;
    end;
    return recnum;
  end;
  function restore_table_ctas(recowner varchar2,
                              rectab varchar2,
                              rstowner varchar2,
                              rsttab varchar2)
  return number
  as
    recnum      number := 0;
    tmptab      varchar2(30);
  begin
    tmptab := gen_table_name(rsttab, '', rstowner);
    begin
      execute immediate 'create table '||rstowner||'.'||tmptab||' as select /*+full(t)*/* from '||recowner||'.'||rectab||' t';
      execute immediate 'insert /*+append*/ into '||rstowner||'.'||rsttab||' select /*+full(t)*/* from '||recowner||'.'||tmptab||' t';
      recnum := SQL%ROWCOUNT;
      execute immediate 'drop table '||rstowner||'.'||tmptab;
      exception when others then
        --trace('[restore_table_ctas] '||SQLERRM);
        --trace('[restore_table_ctas] '||dbms_utility.format_error_backtrace);
        null;
    end;
    return recnum;
  end;
  function restore_table_no_lob(recowner varchar2,
                                rectab varchar2,
                                rstowner varchar2,
                                rsttab varchar2)
  return number
  as
    recnum      number := 0;
    cols        varchar2(32767);
  begin
    cols := get_cols_no_lob(recowner, rectab);
    begin
      --execute immediate 'alter system flush buffer_cache';
      execute immediate 'insert /*+append*/ into '||rstowner||'.'||rsttab||'('||cols||') select /*+full(t)*/'||cols||' from '||recowner||'.'||rectab||' t';
      recnum := recnum + SQL%ROWCOUNT;
    exception when others then
      --raise;
      if sqlcode = -22922 then
        null;
      else
        recnum := recnum + restore_table_in_rows(recowner, rectab, rstowner, rsttab);
      end if;
    end;
    trace('[restore_table_no_lob] '||recnum||' records recovered');
    return recnum;
  end;
  function restore_table( recowner varchar2,
                          rectab varchar2,
                          rstowner varchar2,
                          rsttab varchar2,
                          selflink varchar2 default '')
  return number
  as
    recnum      number := 0;
    expnum      number := 0;
  begin
    begin
      trace('[restore_table] Trying to restore data to '||rstowner||'.'||rsttab);
      execute immediate 'alter system flush buffer_cache';
      if s_tracing then
        execute immediate 'select /*+full(t)*/count(*) from '||recowner||'.'||rectab||' t' into expnum;
        trace('[restore_table] Expected Records in this round: '||expnum);
      end if;
      execute immediate 'insert /*+append*/ into '||rstowner||'.'||rsttab||' select /*+full(t)*/* from '||recowner||'.'||rectab||' t';
      recnum := recnum + SQL%ROWCOUNT;
      if s_tracing and expnum != SQL%ROWCOUNT then
        trace('[restore_table] '||(expnum-SQL%ROWCOUNT)||' records lost!');
        return -1; -- for test
      end if;
    exception when others then
      --raise;
      if sqlcode = -22922 then
        log('Warning: Unrecoverable Lob found!');
        recnum := recnum + restore_table_in_rows_remote(recowner, rectab, rstowner, rsttab, selflink);
        --recnum := recnum + restore_table_no_lob(recowner, rectab, rstowner, rsttab);
      else
        trace(SQLERRM);
        trace('[restore_table] '||dbms_utility.format_error_backtrace);
        --recnum := recnum + restore_table_in_rows_remote(recowner, rectab, rstowner, rsttab, selflink);
        --return -1; -- test
        recnum := recnum + restore_table_in_rows(recowner, rectab, rstowner, rsttab);
      end if;
    end;
    execute immediate 'alter system flush buffer_cache';
    trace('[restore_table] '||recnum||' records recovered');
    return recnum;
  end;
  procedure recover_table(oriobjid number,
                          recowner varchar2,
                          rectab varchar2,
                          rstowner varchar2,
                          rsttab varchar2,
                          srcdir varchar2,
                          srcfile varchar2,
                          recdir varchar2,
                          recfile varchar2,
                          copydir varchar2,
                          copyfile varchar2,
                          blksz number default 8192,
                          fillblks number default 5,
                          selflink varchar2 default '',
                          endianess number default 1)
  as
    -- blk    blob;
    --vrw    raw(32767);
    frw    raw(32767);
    tsz    number := 4;
    hsz    number := 28;
    objr   raw(4);
    objn   number;
    dtail  raw(4);
    dhead  raw(32);
    dbody  raw(32767);
    --bfr    bfile;
    bfo    utl_file.file_type;
    bfr    utl_file.file_type;
    bfw    utl_file.file_type;
    fillednum number := 0;
    dummyheader number;
    dummyblks   number;
    blkstofill  number := fillblks;
    recnum      number := 0;
    rstnum      number := 0;
    i           number := 0;
    j           number := 0;
    truncblks   number := 0;
  begin
    execute immediate 'truncate table '||rstowner||'.'||rsttab;
    execute immediate 'alter system set db_block_checking=false scope=memory';
    execute immediate 'alter system set db_block_checksum=false scope=memory';
    execute immediate 'alter system set "_db_block_check_objtyp"=false scope=memory';
    execute immediate 'alter session set events ''10231 trace name context forever, level 10''';
    execute immediate 'alter session set events ''10233 trace name context forever, level 10''';
    select header_block+1, blocks-3 into dummyheader, dummyblks from dba_segments where owner = recowner and segment_name = rectab;
    if blkstofill > dummyblks then
      blkstofill := dummyblks;
    end if;
    bfo := utl_file.fopen(srcdir, srcfile, 'RB');
    --utl_file.get_raw(bfo, dbody, blksz-hsz-tsz);
    --utl_file.get_raw(bfo, dtail, tsz);
    bfr := utl_file.fopen(copydir, copyfile, 'RB');
    bfw := utl_file.fopen(recdir, recfile, 'WB');
    -- reach to the transaction blocks to be filled
    i := 0;
    while true loop
    begin
      utl_file.get_raw(bfr, frw, blksz);
      utl_file.put_raw(bfw, frw);
      i := i+1;
      exit when i=dummyheader+fillednum;
      exception when others then
        --raise;
        --trace('[recover_table] block NO.: '||i);
        exit;
    end;
    end loop;
    -- go through the data file of truncated table
    while true loop
    begin
      --trace('[recover_table] '||j);
      j := j+1;
      --objr := substrb(rawtohex(dhead), 49, 8);
      utl_file.get_raw(bfo, dhead, hsz);
      if hsz <= 24 then
        utl_file.get_raw(bfo, dbody, blksz-tsz-hsz);
        --objr := substrb(rawtohex(dbody), 49-hsz*2, 8);
        objr := utl_raw.substr(dbody, 25-hsz, 4);
      else
        --objr := substrb(rawtohex(dhead), 49, 8);
        objr := utl_raw.substr(dhead, 25, 4);
      end if;
      if endianess > 0 then
        --objn := to_number(utl_raw.reverse(hextoraw(objr)), 'XXXXXXXX');
        objn := to_number(rawtohex(utl_raw.reverse(objr)), 'XXXXXXXX');
      else
        --objn := to_number(hextoraw(objr), 'XXXXXXXX');
        objn := to_number(rawtohex(objr), 'XXXXXXXX');
      end if;
      -- check if block belongs to truncated table
      if objn != oriobjid or substrb(rawtohex(dhead), 1, 2) != '06' then
        if hsz > 24 then
          utl_file.get_raw(bfo, dbody, blksz-hsz);
        else
          utl_file.get_raw(bfo, dtail, tsz);
        end if;
      else
        --trace('[recover_table] Find it.');
        truncblks := truncblks + 1;
        if hsz > 24 then
          utl_file.get_raw(bfo, dbody, blksz-hsz-tsz);
        end if;
        utl_file.get_raw(bfo, dtail, tsz);
        if not utl_file.is_open(bfr) then
          bfr := utl_file.fopen(copydir, copyfile, 'RB');
        end if;
        if not utl_file.is_open(bfw) then
          bfw := utl_file.fopen(recdir, recfile, 'WB');
        end if;
        -- filling the trans block
        utl_file.get_raw(bfr, dhead, hsz);
        utl_file.put_raw(bfw, dhead); -- put original header
        utl_file.put_raw(bfw, dbody); -- replace body
        utl_file.get_raw(bfr, dbody, blksz-hsz-tsz); -- forward pointer in original file copy
        utl_file.get_raw(bfr, dtail, tsz); -- get original tail
        utl_file.put_raw(bfw, dtail); -- put original tail
        fillednum := fillednum+1;
        i := i+1;
        -- no trans data block left, copy recovered data to backup table and fill the left blocks
        if fillednum >= blkstofill then
        --if fillednum+blkstofill-1 >= dummyblks then
        begin
          while true loop
          begin
            utl_file.get_raw(bfr, frw, blksz);
            utl_file.put_raw(bfw, frw);
            i := i+1;
            exception when others then
              if utl_file.is_open(bfr) then
                utl_file.fclose(bfr);
              end if;
              if utl_file.is_open(bfw) then
                utl_file.fclose(bfw);
              end if;
              exit;
          end;
          end loop;
          rstnum := restore_table(recowner, rectab, rstowner, rsttab, selflink);
          -- for test
          exit when rstnum < 0;
          recnum := recnum+rstnum;
          fillednum := 0;
          commit;
          bfr := utl_file.fopen(copydir, copyfile, 'RB');
          bfw := utl_file.fopen(recdir, recfile, 'WB');
          -- go to the transaction blocks again
          i := 0;
          while true loop
          begin
            utl_file.get_raw(bfr, frw, blksz);
            utl_file.put_raw(bfw, frw);
            i := i+1;
            exit when i=dummyheader+fillednum;
            exception when others then
              --raise;
              --trace('[recover_table] block NO.: '||i);
              exit;
          end;
          end loop;
          utl_file.fflush(bfw);
          exception when others then
            trace('[recover_table 2-1] '||sqlerrm);
            trace('[recover_table 2-1] '||dbms_utility.format_error_backtrace);
            null;
          end;
        end if;
      end if;
      exception
        when no_data_found then
          exit;
        when others then
          trace('[recover_table 2-2] '||sqlerrm);
          trace('[recover_table 2-2] '||dbms_utility.format_error_backtrace);
          exit;
    end;
    end loop;
    -- last blocks not full filled dummy table
    --if fillednum+blkstofill-1 < dummyblks then
    if fillednum < blkstofill and rstnum>=0 then
    begin
      while true loop
      begin
        utl_file.get_raw(bfr, frw, blksz);
        utl_file.put_raw(bfw, frw);
        i := i+1;
        exception when others then
          if utl_file.is_open(bfr) then
            utl_file.fclose(bfr);
          end if;
          if utl_file.is_open(bfw) then
            utl_file.fclose(bfw);
          end if;
          exit;
      end;
      end loop;
      recnum := recnum+restore_table(recowner, rectab, rstowner, rsttab, selflink);
      --fillednum := 0;
      commit;
    end;
    end if;
    if utl_file.is_open(bfr) then
      utl_file.fclose(bfr);
    end if;
    if utl_file.is_open(bfw) then
      utl_file.fclose(bfw);
    end if;
    if utl_file.is_open(bfo) then
      utl_file.fclose(bfo);
    end if;
    utl_file.fclose_all();
    execute immediate 'alter session set events ''10233 trace name context off''';
    execute immediate 'alter session set events ''10231 trace name context off''';
    execute immediate 'alter system set "_db_block_check_objtyp"=true scope=memory';
    execute immediate 'alter system set db_block_checksum=true scope=memory';
    execute immediate 'alter system set db_block_checking=true scope=memory';
    log(truncblks||' truncated data blocks found. ');
    log(recnum||' records recovered in backup table '||rstowner||'.'||rsttab);
  end;
  procedure get_seg_meta( segowner varchar2,
                          segname varchar2,
                          srcdir varchar2,
                          dtail out raw,
                          addinfo out raw,
                          blksz number default 8192)
  as
    frw    raw(32767);
    firstblk  number;
    hdfile varchar2(255);
    bfo    utl_file.file_type;
    i      number := 0;
  begin
    select header_block+1 into firstblk from dba_segments where owner = segowner and segment_name = segname;
    select substr(file_name,decode(instr(d.file_name, '\', -1), 0, instr(file_name, '/', -1), instr(file_name, '\', -1))+1) into hdfile from dba_data_files d, dba_segments s where s.header_file = d.file_id and s.owner = segowner and s.segment_name = segname;
    bfo := utl_file.fopen(srcdir, hdfile, 'RB');
    -- reach to the truncated data blocks
    i := 0;
    while true loop
    begin
      utl_file.get_raw(bfo, frw, blksz);
      i := i+1;
      exit when i = firstblk;
      exception when others then
        exit;
    end;
    end loop;
    utl_file.get_raw(bfo, frw, blksz);
    dtail := utl_raw.substr(frw, blksz-3, 4);
    addinfo := utl_raw.substr(frw, 39, 2);
    utl_file.fclose(bfo);
  end;
  function get_seg_data_id( segowner varchar2,
                            segname varchar2,
                            srcdir varchar2,
                            blksz number default 8192,
                            endianess number default 1)
  return number
  as
    frw    raw(32767);
    hsz    number := 28;
    firstblk  number;
    hdfile varchar2(255);
    bfo    utl_file.file_type;
    i      number := 0;
    objr   raw(4);
    objn   number;
  begin
    select header_block+1 into firstblk from dba_segments where owner = segowner and segment_name = segname;
    select substr(file_name,decode(instr(d.file_name, '\', -1), 0, instr(file_name, '/', -1), instr(file_name, '\', -1))+1) into hdfile from dba_data_files d, dba_segments s where s.header_file = d.file_id and s.owner = segowner and s.segment_name = segname;
    bfo := utl_file.fopen(srcdir, hdfile, 'RB');
    -- reach to the truncated data blocks
    i := 0;
    while true loop
    begin
      utl_file.get_raw(bfo, frw, blksz);
      i := i+1;
      exit when i = firstblk;
      exception when others then
        exit;
    end;
    end loop;
    utl_file.get_raw(bfo, frw, hsz);
    objr := utl_raw.substr(frw, 25, 4);
    if endianess > 0 then
      objn := to_number(rawtohex(utl_raw.reverse(objr)), 'XXXXXXXX');
    else
      objn := to_number(rawtohex(objr), 'XXXXXXXX');
    end if;
    utl_file.fclose(bfo);
    return objn;
  end;
  procedure recover_table(tgtowner varchar2,
                          tgttable varchar2,
                          recowner varchar2,
                          rectab varchar2,
                          rstowner varchar2,
                          rsttab varchar2,
                          srcdir varchar2,
                          recdir varchar2,
                          recfile varchar2,
                          copydir varchar2,
                          copyfile varchar2,
                          blksz number default 8192,
                          fillblks number default 5,
                          selflink varchar2 default '')
  as
    tgtobjid    number;
    recobjid    number;
    endianess   number;
    tmpcopyf    varchar2(256);
    tsname      varchar2(30);
    readprop    varchar2(30);
    dtail       raw(4);
    addinfo     raw(32);
  begin
    select instr(platform_name, 'Windows') into endianess from v$database where rownum<=1;
    select data_object_id into recobjid from dba_objects where owner = recowner and object_name = rectab and object_type='TABLE' and rownum<=1;
    log('begin to recover table '||tgtowner||'.'||tgttable);
    tgtobjid := get_seg_data_id(tgtowner, tgttable, srcdir, blksz, endianess);
    if s_repobjid then
      get_seg_meta(recowner, rectab, srcdir, dtail, addinfo, blksz);
      select tablespace_name into tsname from dba_tables where owner = tgtowner and table_name = tgttable and rownum<=1;
      select STATUS into readprop from dba_tablespaces where tablespace_name = tsname;
      if readprop != 'READ ONLY' then
        execute immediate 'alter tablespace '||tsname||' read only';
        execute immediate 'alter system flush buffer_cache';
      end if;
      for file_rec in (select substr(file_name,decode(instr(d.file_name, '\', -1), 0, instr(file_name, '/', -1), instr(file_name, '\', -1))+1) as filename from dba_data_files d, dba_tables t where d.tablespace_name = t.tablespace_name and t.owner = tgtowner and t.table_name = tgttable) loop
      begin
        tmpcopyf := gen_file_name(file_rec.filename, '$');
        copy_file(srcdir, file_rec.filename, srcdir, tmpcopyf);
        --replace_segmeta_in_file(srcdir, tmpcopyf, srcdir, file_rec.filename, tgtobjid, recobjid, dtail, 39, addinfo, blksz, endianess);
        replace_segmeta_in_file(srcdir, tmpcopyf, srcdir, file_rec.filename, tgtobjid, recobjid, dtail, 39, '', blksz, endianess);
        recover_table(recobjid, recowner, rectab, rstowner, rsttab, srcdir, file_rec.filename, recdir, recfile, copydir, copyfile, blksz, fillblks, selflink, endianess);
        --recover_table(tgtobjid, recowner, rectab, rstowner, rsttab, srcdir, file_rec.filename, recdir, recfile, copydir, copyfile, blksz, fillblks, selflink, endianess);
        copy_file(srcdir, tmpcopyf, srcdir, file_rec.filename);
        utl_file.fremove(srcdir, tmpcopyf);
        trace('[recover_table 1] '||tmpcopyf||' removed.');
      exception when others then
        trace('[recover_table 1] '||sqlerrm);
        trace('[recover_table 1] '||dbms_utility.format_error_backtrace);
      end;
      end loop;
      if readprop != 'READ ONLY' then
        execute immediate 'alter tablespace '||tsname||' read write';
      end if;
    else
      for file_rec in (select substr(file_name,decode(instr(d.file_name, '\', -1), 0, instr(file_name, '/', -1), instr(file_name, '\', -1))+1) as filename from dba_data_files d, dba_tables t where d.tablespace_name = t.tablespace_name and t.owner = tgtowner and t.table_name = tgttable) loop
      begin
        recover_table(tgtobjid, recowner, rectab, rstowner, rsttab, srcdir, file_rec.filename, recdir, recfile, copydir, copyfile, blksz, fillblks, selflink, endianess);
      exception when others then
        trace('[recover_table 1] '||sqlerrm);
        trace('[recover_table 1] '||dbms_utility.format_error_backtrace);
      end;
      end loop;
    end if;
    log('Recovery completed.');
  end;
  procedure prepare_files(tgtowner varchar2,
                          tgttable varchar2,
                          datapath varchar2,
                          datadir in out varchar2,
                          rects out varchar2,
                          recfile out varchar2,
                          rstts out varchar2,
                          rstfile out varchar2,
                          blksz out varchar2,
                          rectsblks number default 16,
                          rsttsblks number default 2560)
  as
    ext_mgmt   varchar2(30);
    ss_mgmt    varchar2(30);
  begin
    select block_size, extent_management, segment_space_management into blksz, ext_mgmt, ss_mgmt from dba_tablespaces ts, dba_tables t where t.tablespace_name = ts.tablespace_name and t.owner = upper(tgtowner) and t.table_name = upper(tgttable);
    select datadir||surfix into datadir from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_directories where directory_name = datadir||surfix) order by surfix nulls first) where rownum<=1;
    log('Directory Name: '||datadir);
    execute immediate 'create directory '||datadir||' as '''||datapath||'''';
    --select 'FY_REC_DATA'||surfix into rects from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_tablespaces where tablespace_name = 'FY_REC_DATA'||surfix) order by surfix nulls first) where rownum<=1;
    --select 'FY_REC_DATA'||surfix||'.DAT' into recfile from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_data_files where file_name like '%\FY_REC_DATA'||surfix||'.DAT') order by surfix nulls first) where rownum<=1;
    rects := gen_ts_name('FY_REC_DATA','');
    recfile := gen_file_name('FY_REC_DATA','');
    log('Recover Tablespace: '||rects||'; Data File: '||recfile);
    execute immediate 'create tablespace '||rects||' datafile '''||rtrim(datapath, '\')||'\'||recfile||''' size '||to_char(blksz*rectsblks/1024)||'K autoextend off extent management '||ext_mgmt||' SEGMENT SPACE MANAGEMENT '||ss_mgmt;
    select 'FY_RST_DATA'||surfix into rstts from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_tablespaces where tablespace_name = 'FY_REST_DATA'||surfix) order by surfix nulls first) where rownum<=1;
    --select 'FY_RST_DATA'||surfix||'.DAT' into rstfile from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_data_files where file_name like '%\FY_REST_DATA'||surfix||'.DAT') order by surfix nulls first) where rownum<=1;
    rstts := gen_ts_name('FY_RST_DATA','');
    rstfile := gen_file_name('FY_RST_DATA','');
    log('Restore Tablespace: '||rstts||'; Data File: '||rstfile);
    execute immediate 'create tablespace '||rstts||' datafile '''||rtrim(datapath, '\')||'\'||rstfile||''' size '||to_char(blksz*rsttsblks/1024)||'K autoextend on extent management '||ext_mgmt||' SEGMENT SPACE MANAGEMENT '||ss_mgmt;
  end;
  procedure clean_up_ts(rects varchar2,
                        rstts varchar2 default null)
  as
    readprop varchar2(30);
  begin
    select STATUS into readprop from dba_tablespaces where tablespace_name = rects;
    if readprop = 'READ ONLY' then
      execute immediate 'alter tablespace '||rects||' read write';
    end if;
    for tab_rec in (select owner, table_name from dba_tables where tablespace_name = rects) loop
      execute immediate 'drop table '||tab_rec.owner||'.'||tab_rec.table_name;
    end loop;
    if rstts is not null then
      for tab_rec in (select owner, table_name from dba_tables where tablespace_name = rstts) loop
        execute immediate 'drop table '||tab_rec.owner||'.'||tab_rec.table_name;
      end loop;
    end if;
  end;
  procedure fill_blocks(tgtowner varchar2,
                        tgttable varchar2,
                        datadir varchar2,
                        rects varchar2,
                        recfile varchar2,
                        rstts varchar2,
                        blks number default 8,
                        recowner varchar2 default user,
                        rstowner varchar2 default user,
                        rectab in out varchar2,
                        rsttab in out varchar2,
                        copyfile out varchar2)
  as
    blksz  number;
    blkno  number;
    cols   varchar2(32767);
    vals   varchar2(32767);
    colno  number := 0;
  begin
    if rectab is null then
      select block_size into blksz from dba_tablespaces ts, dba_tables t where t.tablespace_name = ts.tablespace_name and t.owner = upper(tgtowner) and t.table_name = upper(tgttable);
      -- select upper(tgttable||'$'||surfix) into rectab from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_tables where owner = recowner and table_name = upper(tgttable||'$'||surfix)) order by surfix nulls first) where rownum<=1;
      rectab := gen_table_name(tgttable, '$', recowner);
      log('Recover Table: '||recowner||'.'||rectab);
      --trace('[fill_blocks] create table '||recowner||'.'||rectab||' tablespace '||rects||' storage(initial '||to_char(blks*blksz/1024)||'K) as select * from '||tgtowner||'.'||tgttable||' where 1=2');
      execute immediate 'create table '||recowner||'.'||rectab||' tablespace '||rects||' storage(initial '||to_char(blks*blksz/1024)||'K) as select * from '||tgtowner||'.'||tgttable||' where 1=2';
    else
      --execute immediate 'truncate table '||recowner||'.'||rectab;
      execute immediate 'delete from '||recowner||'.'||rectab;
      commit;
    end if;
    cols := '';
    vals := '';
    for col_rec in (select column_name, data_type, nullable from dba_tab_cols where owner = recowner and table_name = rectab) loop
      if col_rec.nullable = 'N' then
        execute immediate 'alter table '||recowner||'.'||rectab||' modify '||col_rec.column_name||' null';
      end if;
      if colno < 6 then
        if col_rec.data_type like '%CHAR%' or col_rec.data_type like '%RAW%' then
          if colno > 0 then
            cols := cols||',';
            vals := vals||',';
          end if;
          cols := cols||col_rec.column_name;
          vals := vals||'''A''';
          colno := colno + 1;
       elsif col_rec.data_type like '%NUMBER%' or col_rec.data_type = 'FLOAT' then
          if colno > 0 then
            cols := cols||',';
            vals := vals||',';
          end if;
          cols := cols||col_rec.column_name;
          vals := vals||'0';
          colno := colno + 1;
       elsif col_rec.data_type LIKE '%TIMESTAMP%' or col_rec.data_type = 'DATE' then
          if colno > 0 then
            cols := cols||',';
            vals := vals||',';
          end if;
          cols := cols||col_rec.column_name;
          vals := vals||'sysdate';
          colno := colno + 1;
       end if;
      end if;
    end loop;
    --select upper(tgttable||'$$'||surfix) into rsttab from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_tables where owner = rstowner and table_name = upper(tgttable||'$$'||surfix)) order by surfix nulls first) where rownum<=1;
    if rsttab is null then
      rsttab := gen_table_name(tgttable, '$$', rstowner);
      log('Restore Table: '||rstowner||'.'||rsttab);
      execute immediate 'create table '||rstowner||'.'||rsttab||' tablespace '||rstts||' as select * from '||recowner||'.'||rectab||' where 1=2';
    else
      execute immediate 'truncate table '||rstowner||'.'||rsttab;
    end if;
    --trace('[fill_blocks] insert into '||recowner||'.'||rectab||'('||cols||') values ('||vals||')');
    while true loop
      execute immediate 'insert into '||recowner||'.'||rectab||'('||cols||') values ('||vals||')';
      execute immediate 'select count(unique(dbms_rowid.rowid_block_number( rowid ))) from '||recowner||'.'||rectab into blkno ;
      exit when blkno >= blks-3;
    end loop;
    commit;
    execute immediate 'alter system flush buffer_cache';
    execute immediate 'delete from '||recowner||'.'||rectab;
    commit;
    execute immediate 'alter system flush buffer_cache';
    trace('[fill_blocks] Data Blocks formatted.');
    execute immediate 'alter tablespace '||rects||' read only';
    select 'FY_REC_DATA_COPY'||surfix||'.DAT' into copyfile from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_data_files where file_name like '%FY_REC_DATA_COPY'||surfix||'.DAT') order by surfix nulls first) where rownum<=1;
    copy_file(datadir, recfile, datadir, copyfile);
    log('Copy file of Recover Tablespace: '||copyfile);
  end;
  procedure test_rec1( tow varchar2 default 'SYS',
                       ttb varchar2 default 'T_CHAIN',
                       fbks number default 1,
                       datapath varchar2 default 'D:\oracle\product\10.2.0\oradata\EDGAR\DATAFILE\')
  as
    tgtowner varchar2(30):= upper(tow);
    tgttable varchar2(30):= upper(ttb);
    datadir varchar2(30);
    rects varchar2(30);
    recfile varchar2(30);
    rstts varchar2(30);
    rstfile varchar2(30);
    blksz number;
    rectab varchar2(30);
    rsttab varchar2(30);
    copyfile varchar2(30);
  begin
    datadir := 'FY_DATA_DIR';
    prepare_files(tgtowner, tgttable, datapath, datadir, rects, recfile, rstts, rstfile, blksz);
    rects := 'FY_REC_DATA';
    rstts := 'FY_RST_DATA';
    recfile := 'FY_REC_DATA.DAT';
    fill_blocks(tgtowner, tgttable, datadir, rects, recfile, rstts, 8, tgtowner, tgtowner, rectab, rsttab, copyfile);
    recover_table(tgtowner, tgttable, tgtowner, rectab, tgtowner, rsttab, datadir, datadir, recfile, datadir, copyfile, blksz, fbks, 'myself');
  end;
  procedure test_rec2( tow varchar2 default 'SYS',
                       ttb varchar2 default 'T_CHAIN',
                       fbks number default 1)
  as
    tgtowner varchar2(30):= upper(tow);
    tgttable varchar2(30):= upper(ttb);
    datadir varchar2(30);
    rects varchar2(30);
    recfile varchar2(30);
    rstts varchar2(30);
    blksz number;
    rectab varchar2(30);
    rsttab varchar2(30);
    copyfile varchar2(30);
  begin
    datadir := 'FY_DATA_DIR';
    rects := 'FY_REC_DATA';
    rstts := 'FY_RST_DATA';
    recfile := 'FY_REC_DATA.DAT';
    clean_up_ts(rects, rstts);
    select block_size into blksz from dba_tablespaces ts, dba_tables t where ts.tablespace_name = t.tablespace_name and t.owner = tgtowner and t.table_name = tgttable;
    fill_blocks(tgtowner, tgttable, datadir, rects, recfile, rstts, 8, tgtowner, tgtowner, rectab, rsttab, copyfile);
    recover_table(tgtowner, tgttable, tgtowner, rectab, tgtowner, rsttab, datadir, datadir, recfile, datadir, copyfile, blksz, fbks, 'myself');
  end;
begin
  null;
end pg_lan_recover_tab_data;
/

ORACLE恢复truncate的表-转载相关推荐

  1. Oracle的闪回特性之恢复truncate删除表的数据

    /* 2008/06/06 *环境:Windows XP +Oracle10.2.0.1 *循序渐进oracle--数据库管理.优化与备份恢复 *循序渐进oracle第8章:Oracle的闪回特性之恢 ...

  2. oracle恢复误删的表

    一:表的恢复      对误删的表,只要没有使用PURGE永久删除选项,那么从flash back区恢复回来希望是挺大的.一般步骤有: 1.从flash back里查询被删除的表     select ...

  3. oracle 恢复 跳过 表空间,Oracle表空间恢复

    如果误删除了一个表空间中test的数据文件test.dbf,然后下次重新启动数据库的时候发      数据库不能打开了(open), 老是报错:SQL> alter database open; ...

  4. php delete和truncate,TRUNCATE 删除表,无法回退。默认选择为整个表的内容,所以不能加条件。...

    TRUNCATE 删除表,无法回退.默认选择为整个表的内容,所以不能加条件. DELETE 删除表,可以回退.可以带where 条件.建议使用delete.但是TRUNCATE 删除表数据比delet ...

  5. 案例:Oracle dul数据挖掘 没有数据库备份非常规恢复truncate删除的数据表

    Oracle数据库在没有备份情况下在对表中的某数据表进行truncate删除后,通过oracle dul进行非常规恢复 1.准备oracle dul测试环境 SQL> select count( ...

  6. oracle truncate可以恢复吗,恢复truncate表

    如果数据库是非归档模式且没有备份,此时如果drop掉一张表,有办法可以恢复吗?当然可以,而且是完全恢复:http://blog.sina.com.cn/s/blog_61cd89f60102ed97. ...

  7. oracle dul误删数据,案例:Oracle dul数据挖掘 没有数据库备份非常规恢复truncate删除的数据表...

    Oracle数据库在没有备份情况下在对表中的某数据表进行truncate删除后,通过oracle dul进行非常规恢复 1.准备oracle dul测试环境SQL> select count(* ...

  8. oracle恢复drop建的表首次,Oracle中Drop,Delete,Truancate表恢复

    Oracle中Drop,delete,truancate表恢复 oracle中,常常会由于一些失误导致表的删除,以下是我写的一些表恢复的方法. 闪回模式得满足条件(启用闪回区和启用归档): 1.检查是 ...

  9. 使用Fy_Recover_data恢复被truncate的表

    参考文档: Fy_Recover_Data ---- 用于数据恢复的PLSQL包 [HelloDBA.COM] 移花接木----利用Oracle表扫描机制恢复被Truncate的数据 [HelloDB ...

最新文章

  1. Cisco2811基本操作
  2. C#返回mysql查询结果_c#查询MySQL是怎么接收返回结果的
  3. python requests超时时间_python - requests 库 使用过程中timeout值最大可设值?
  4. Java黑皮书课后题第2章:2.1(将摄氏温度转换为华氏温度)编写程序,从控制台读入double型的摄氏温度值,将其转为华氏温度,显示结果
  5. GridCtrl学习笔记(2)寻找自动更新表格的最新数据并把其显示到当前窗口的方法...
  6. STP/RSTP/MSTP协议简介
  7. 每日一题:leetcode80.删除有序数组中的重复元素贰
  8. oracle有sysdata,Oracle基础
  9. 信息奥赛一本通(1096:数字统计)
  10. Spring Boot 事务支持
  11. 使用MemCache进行相关缓存的保存处理
  12. html5画布作品,10个会让你惊叹不已的HTML5画布(CANVAS)技术应用演
  13. python计算工资编程-Python工程师薪水居然这么高
  14. Java中的System.exit()
  15. linux需要添加一个根分区才能安装,linux 根分区扩展
  16. Unity3D中2D图片动画进行帧动画播放
  17. Presenting view controllers on detached view controllers is discouraged
  18. 软件工程第2次作业 | 结对项目-最长单词链
  19. 一网通支付ios开发
  20. 家政服务:保姆朋友圈鄙视链在上海

热门文章

  1. 西北农林科技大学linux实验,2021双非科班调剂985(一志愿中科大,调剂上岸西北农林科技大学)初试复试经验帖...
  2. 数字转化为16进制数
  3. 任性!C语言之父:因拒付论文装订费错失博士学位,论文52年后重见天日
  4. [导入]n73手机拼音输入法
  5. webexam php,Laravel View Composer - 当 include 一个模板时,自动获取其所需的变量
  6. 如何利用计算机完成一篇文稿制作,怎么制作美篇-教你打造一篇原创内容撰写步骤...
  7. 嵌入式-ARM-学习总结(1):初识ARM
  8. Android 加壳与脱壳方式总结
  9. LambdaMart一些浅薄理解
  10. python中如何进行测试