如何恢複一個被誤drop的預存程序

來源:互聯網
上載者:User

今天有同事給我寫信:"我大概10分鐘前錯誤地drop掉了一個預存程序:P_IPACCHECK_NC,而這個預存程序的源碼我本機又沒有備份,麻煩您恢複一下,謝謝" 

這種恢複是非常容易的,原理就是利用了Oracle裡所有的預存程序的原始碼都是存在dba_source裡,而drop某個預存程序的時候,oracle這裡肯定要去dba_source裡把相關的原始碼給delete掉,既然是delete,那就好辦咯,直接flashback query就可以了。 

如下是完整的恢複過程:

用sys使用者登陸,執行如下的查詢:

SQL> select text from dba_source as of timestamp to_timestamp('2009-03-06 09:45:00', 'YYYY-MM-DD HH24:MI:SS') where owner='IPRA' and name= 'P_IPACCHECK_NC' order by line;

TEXT

--------------------------------------------------------------------------------

procedure P_IPACCHECK_NC(n_flag     out number,

                                           vc_message out varchar2) is

  ------------------------------------------------------------------------------

  --    PROCEDURE NAME      : P_IPACCHECK_NC                       --

  --    NAME IN SYSMTH      : NONE                                --

  --    DESCRIPTION         : 對IWBIBT記錄進行有效性檢查,沒有錯誤的資料置標誌為

  --

  --       INVOKED          :                                     --

  --    PROGRAMMED BY       : ZhouXin         DATE  2008/12/02    --

  --     MODIFIED BY        :

  --         TYPE           : ONLINE                              --

  --                   COPYRIGHT 1997~2008 ACCA-ARK               --

  --                                                              --

  ------------------------------------------------------------------------------

  vc_ipastc  varchar2(20);

  n_errcount number := 0;

begin

  for rec_pac in (select * from iwbpac where ipastc is null) loop

 

TEXT

--------------------------------------------------------------------------------

    n_errcount := 0;

    vc_ipastc  := rec_pac.ipastc;

    --檢查清算月

    if rec_pac.ipalrm > to_number(to_char(sysdate, 'YYYYMM')) then

      vc_ipastc  := vc_ipastc || 'A';

      n_errcount := n_errcount + 1;

    end if;

    --檢查名義開賬公司

    if f_masaln_existawbprefix(rec_pac.ipaarr) != true then

      vc_ipastc  := vc_ipastc || 'B';

      n_errcount := n_errcount + 1;

    end if;

    --檢查實際開賬公司

    if f_masaln_existawbprefix(rec_pac.ipacar) != true then

      vc_ipastc  := vc_ipastc || 'C';

      n_errcount := n_errcount + 1;

    end if;

    --檢查開賬公司

    if f_masaln_existawbprefix(rec_pac.ipairl) != true then

      vc_ipastc  := vc_ipastc || 'E';

      n_errcount := n_errcount + 1;

 

TEXT

--------------------------------------------------------------------------------

    end if;

    --檢查名義開賬公司

    if rec_pac.ipalas <> 'P' then

      vc_ipastc  := vc_ipastc || 'F';

      n_errcount := n_errcount + 1;

    end if;

    --檢查帳單錄入日期

    if rec_pac.ipanpd > to_number(to_char(sysdate, 'YYYYMMDD')) then

      vc_ipastc  := vc_ipastc || 'G';

      n_errcount := n_errcount + 1;

    end if;

    --檢查開賬月

    if rec_pac.ipailm > to_number(to_char(sysdate, 'YYYYMM')) then

      vc_ipastc  := vc_ipastc || 'H';

      n_errcount := n_errcount + 1;

    end if;

    --檢查原始開賬金額

    if rec_pac.ipaemk = 'B' and rec_pac.ipaamt is null then

      vc_ipastc  := vc_ipastc || 'I';

      n_errcount := n_errcount + 1;

    end if;

 

TEXT

--------------------------------------------------------------------------------

    --檢查清算期

    if to_number(rec_pac.ipacpr) < 1 or to_number(rec_pac.ipacpr) > 4 then

      vc_ipastc  := vc_ipastc || 'J';

      n_errcount := n_errcount + 1;

    end if;

    --檢查開賬期

    if to_number(rec_pac.ipabpr) < 1 or to_number(rec_pac.ipabpr) > 4 then

      vc_ipastc  := vc_ipastc || 'K';

      n_errcount := n_errcount + 1;

    end if;

    --沒有錯誤,置標誌位'0'

    if n_errcount = 0 then

      update iwbpac

         set ipastc = '0'

       where ipacpr = rec_pac.ipacpr

         and ipairl = rec_pac.ipairl

         and ipacar = rec_pac.ipacar

         and ipanvn = rec_pac.ipanvn

         and ipanva = rec_pac.ipanva

         and ipalrm = rec_pac.ipalrm;

    else

 

TEXT

--------------------------------------------------------------------------------

      update iwbpac

         set ipastc = vc_ipastc

       where ipacpr = rec_pac.ipacpr

         and ipairl = rec_pac.ipairl

         and ipacar = rec_pac.ipacar

         and ipanvn = rec_pac.ipanvn

         and ipanva = rec_pac.ipanva

         and ipalrm = rec_pac.ipalrm;

    end if;

  end loop;

exception

  when others then

    n_flag     := 0;

    vc_message := substr(sqlerrm, 1, 1000);

end P_IPACCHECK_NC;

 

100 rows selected

 

補充:

sys@ORCL> select text from dba_source where owner='LSF' and name='EMP_SAL' order by line;

TEXT
-----------------------------------------------------------------------------------------------------------------------------------
procedure emp_sal
is
v_last_name employee.last_name%type;
v_employee_id employee.employee_id%type;
v_salary employee.salary%type;
cursor cursor_sal is
select last_name,employee_id,salary from employee where salary between 2000 and 3000;
begin
open cursor_sal;
loop
fetch cursor_sal into v_last_name,v_employee_id,v_salary;
exit when cursor_sal%notfound;
update employee set salary=salary*1.2 where last_name=v_last_name and employee_id=v_employee_id;
end loop;
close cursor_sal;
commit;
end;

17 rows selected.

 

SQL> show user
USER is "LSF"
SQL> select username from user_users;

USERNAME
------------------------------
LSF

SQL> select text from user_source where name='EMP_SAL' order by line;

TEXT
--------------------------------------------------------------------------------
procedure emp_sal
is
v_last_name employee.last_name%type;
v_employee_id employee.employee_id%type;
v_salary employee.salary%type;
cursor cursor_sal is
select last_name,employee_id,salary from employee where salary between 2000 and
3000;

begin
open cursor_sal;
loop
fetch cursor_sal into v_last_name,v_employee_id,v_salary;
exit when cursor_sal%notfound;
update employee set salary=salary*1.2 where last_name=v_last_name and employee_i
d=v_employee_id;

end loop;
close cursor_sal;
commit;
end;

17 rows selected.

SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

TO_CHAR(SYSDATE,'YY
-------------------
2011-08-10 14:46:24

SQL> drop procedure emp_sal;

Procedure dropped.

SQL> select text from user_source where name='EMP_SAL' order by line;

no rows selected

SQL> select text from user_source as of timestamp to_timestamp('2011-08-10 14:46:24','YYYY-MM-DD HH24:MI:SS') where name='EMP_SAL' order by line;
select text from user_source as of timestamp to_timestamp('2011-08-10 14:46:24','YYYY-MM-DD HH24:MI:SS') where name='EMP_SAL' order by line
                 *
ERROR at line 1:
ORA-01031: insufficient privileges

sys@ORCL> select text from  dba_source as of timestamp to_timestamp('2011-08-10 14:46:24','YYYY-MM-DD HH24:MI:SS') where owner='LSF' and name='EMP_SAL' order by line;

TEXT
-----------------------------------------------------------
procedure emp_sal
is
v_last_name employee.last_name%type;
v_employee_id employee.employee_id%type;
v_salary employee.salary%type;
cursor cursor_sal is
select last_name,employee_id,salary from employee where salary between 2000 and 3000;
begin
open cursor_sal;
loop
fetch cursor_sal into v_last_name,v_employee_id,v_salary;
exit when cursor_sal%notfound;
update employee set salary=salary*1.2 where last_name=v_last_name and employee_id=v_employee_id;
end loop;
close cursor_sal;
commit;
end;

17 rows selected.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.