Oracle_用預存程序殺掉session解決鎖表的問題

來源:互聯網
上載者:User

鎖表,相信大家都遇到過,解鎖的方法想必大家也都知道,但是一個個的殺進程難免有些低級,我們用預存程序來實現這一功能,就變得異常簡單了。

1.建立視圖,查詢進程資訊,

create or replace view mysession asselect b.sid,b.serial# from v$lock a,v$session b where a.SID = b.SID and status='INACTIVE' --and program<>'plsqldev.exe'  order by machine;

2.建立預存程序,迴圈上邊試圖中的資料,一一kill,

create or replace procedure kill_session is  v_sid    varchar2(30);  v_serial varchar2(30);  v_sql    varchar2(1000);  TYPE DyData IS REF CURSOR;  rows DyData;begin  v_sql := 'select * from mysession ';  OPEN rows FOR v_sql;  LOOP    FETCH rows      into v_sid, v_serial;      v_sql := 'alter system kill session ''' || v_sid || ',' || v_serial || '''';    execute immediate v_sql;    --dbms_output.put_line(v_userid || '__' || v_user || '__' || v_pwd);      EXIT WHEN rows%NOTFOUND;  END LOOP;end kill_session;
3.執行預存程序

begin  -- Call the procedure  kill_session;end;

注意有時候可能會把自己的進程也殺掉,只需要自己修改一下第一步的視圖,把自己排除即可。






[csharp] view plaincopy

    【End】

    相關文章

    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.