原文:http://blog.itpub.net/9240380/viewspace-666622/

SQL> declare
2 v_sid v$session.sid%type; --定义如下两个type类型,用于接收cursor
v_serial# v$session.serial#%type;
3 4 cursor cur_session is select sid,serial# from v$session where program ='plsqldev.exe'; --#定义cursor
5 begin
6 open cur_session; --打开cusor
7 loop --打开游标马上开始循环,因为cursor是一条条取数据的
8 fetch cur_session into v_sid,v_serial#; --把游标的数据放入上面定义的type变量中
9 --根据以上的type变量及游标生成批量杀session的动态sql脚本,并执行
10 execute immediate 'alter system kill session '''||v_sid||','||v_serial#||''' immediate';
11 exit when cur_session%notfound; --要加个异常处理,不然永远是死循环
12 dbms_output.put_line('cursor date have been fetched ending');
13 end loop; --loop也有成双匹配出现
14 close cur_session; --游标处理完后,关闭游标
15 end;
16 /
declare
*
ERROR at line 1:
ORA-00030: User session ID does not exist.
ORA-06512: at line 10

明显杀会话时候,会话不存在。再执行类似的PL/SQL 块

SQL> SET serverout ON
SQL> DECLARE
2 v_ename EMP.ENAME%TYPE;
3 v_salary EMP.SAL%TYPE;
4 CURSOR c_emp IS SELECT ename,sal FROM emp;
5 BEGIN
6 OPEN c_emp;
7 loop
8 exit when c_emp%notfound;
9 FETCH c_emp INTO v_ename,v_salary;
10 DBMS_OUTPUT.PUT_LINE('Salary of Employee: '|| v_ename ||' is '|| v_salary);
11 end loop;
12 CLOSE c_emp;
13 END ;
14 /
Salary of Employee: SMITH is 800
Salary of Employee: ALLEN is 1600
Salary of Employee: WARD is 1250
Salary of Employee: JONES is 2975
Salary of Employee: MARTIN is 1250
Salary of Employee: BLAKE is 2850
Salary of Employee: CLARK is 2450
Salary of Employee: SCOTT is 3000
Salary of Employee: KING is 5000
Salary of Employee: TURNER is 1500
Salary of Employee: ADAMS is 1100
Salary of Employee: JAMES is 4400
Salary of Employee: FORD is 3000
Salary of Employee: MILLER is 1300
Salary of Employee: MILLER is 1300

结果最后一行循环执行了2次,在杀会话plsql中,杀最后一个会话操作也执行了2次,所以会遇到报错。

调整PL/SQL 块语句,将exit when cur_session%notfound;  放在fetch 之后,也就是要循环执行的语句之前就解决了

declare
v_sid v$session.sid%type; --定义如下两个type类型,用于接收cursor
v_serial# v$session.serial#%type;
cursor cur_session is select sid,serial# from v$session where program ='plsqldev.exe'; --#定义cursor
begin
open cur_session; --打开cusor
loop --打开游标马上开始循环,因为cursor是一条条取数据的
fetch cur_session into v_sid,v_serial#; --把游标的数据放入上面定义的type变量中
exit when cur_session%notfound; --要加个异常处理,不然永远是死循环
--根据以上的type变量及游标生成批量杀session的动态sql脚本,并执行
execute immediate 'alter system kill session '''||v_sid||','||v_serial#||''' immediate';
dbms_output.put_line('cursor date have been fetched ending');
end loop; --loop也有成双匹配出现
close cur_session; --游标处理完后,关闭游标
end;
/

SQL> select sid,serial#,status from v$session where program ='plsqldev.exe';

SID         SERIAL#    STATUS
---------- ---------- --------
26         27         INACTIVE
1159       189        INACTIVE

SQL>
SQL> declare
2 v_sid v$session.sid%type; --定义如下两个type类型,用于接收cursor
3 v_serial# v$session.serial#%type;
4 cursor cur_session is select sid,serial# from v$session where program ='plsqldev.exe'; --#定义cursor
5 begin
6 open cur_session; --打开cusor
7 loop --打开游标马上开始循环,因为cursor是一条条取数据的
8 fetch cur_session into v_sid,v_serial#; --把游标的数据放入上面定义的type变量中
9 exit when cur_session%notfound; --要加个异常处理,不然永远是死循环
10 --根据以上的type变量及游标生成批量杀session的动态sql脚本,并执行
11 execute immediate 'alter system kill session '''||v_sid||','||v_serial#||''' immediate';
12 dbms_output.put_line('cursor date have been fetched ending');
13 end loop; --loop也有成双匹配出现
14 close cur_session; --游标处理完后,关闭游标
15 end;
16 /
cursor date have been fetched ending
cursor date have been fetched ending

PL/SQL procedure successfully completed.

SQL> select sid,serial#,status from v$session where program ='plsqldev.exe';

no rows selected

批量kill杀死某些会话session的PL/SQL的更多相关文章

  1. 开发人员需求能kill杀死其它阻塞自己的会话,测试发现需要alter system权限有风险

    模拟开发人员需求,可以杀死其它阻塞自己的会话1.能有查询阻塞会话确认的权限SQL> grant select on v_$session to testa;SQL> grant selec ...

  2. PL/SQL 报错:动态执行表不可访问,本会话的自动统计被禁止。 在执行菜单里你可以禁止统计,或在v$session,v$sesstat 和vSstatname表里获得选择权限。

    现象: 第一次用PL/SQL Developer连接数据库,若用sys用户登录并操作则正常,若用普通用户比如haishu登录并创建一个表则报错“动态执行表不可访问,本会话的自动统计被禁止.在执行菜单里 ...

  3. 锁_rac环境kill锁表会话后出现killed状态(解决)

    原创作品,出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46876961 ra ...

  4. 六.dbms_session(提供了使用PL/SQL实现ALTER SESSION命令)

    1.概述 作用:提供了使用PL/SQL实现ALTER SESSION命令,SET ROLE命令和其他会话信息的方法 .2.包的组成 1).set_identifier说明:用于设置会话的客户ID号.语 ...

  5. 简单PHP会话(session)说明

    现在程序员愈发的不容易了,想要精通,必然要寻本溯源,这其实与目前泛滥的愈发高级的语言以及众多的框架刚好相反,因为它们在尽可能的掩盖本源使其简单,个人称之为程序员学习悖论. 注:作者接触web开发和ph ...

  6. paip.mysql 批量kill 连接.

    paip.mysql 批量kill 连接. 作者Attilax  艾龙,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net ...

  7. PL/SQL中批量执行SQL脚本(不可把所有的语句都复制到New SQL Windows)

    PL/SQL中批量执行SQL脚本,不可把所有的语句都复制到New SQL Window,因为这样会导致缓冲区过大而进程卡死! 最好的办法是将要执行的SQL脚本存放到指定文件中,如C:\insert.s ...

  8. http 会话(session)详解

    会话(session)是一种持久网络协议,在用户(或用户代理)端和服务器端之间创建关联,从而起到交换数据包的作用机制 一.查看session id 可利用相关工具,比如firebug,httpwatc ...

  9. Tensorflow会话Session

    转载自: http://blog.csdn.net/Hanging_Gardens/article/details/72784392 https://www.cnblogs.com/hypnus-ly ...

随机推荐

  1. webform 使用log4net配置

    效果: web.config配置 <configuration> <configSections> <!--log4net日志记录--> <section n ...

  2. 基于SVM.NET的验证码识别算法实现

    工作之余,对这个算法做了一些研究,并成功对验证码进行了识别,对普通验证码识别率在90%左右,识别速度相当快,已基于此做过一些自动查询.提交程序(例如投票.发帖等) ,还上过淘宝店,赚过一笔外快,现将相 ...

  3. 结对编程项目总结(core2组)

    结对编程项目总结(core2组) 作业---四则运算(Core 第二组)   ----by 吴雪晴 PB16061514 齐天杨 PB16060706 一.项目简介 项目的任务为制作一个给(貌似是?) ...

  4. Java Web高性能开发 - 前端高性能

    作者:IBM developerWorks链接:https://www.ibm.com/developerworks/cn/java/j-lo-javawebhiperf1/著作权归作者所有.商业转载 ...

  5. C#des加密算法指定键的大小对于此算法无效

    api接口调用的时候,需要和java的进行加密通信,通信过程中用到DES加密,java那边DES的key为64位字符串,而之前c#的DES加密是key为8位 DESCryptoServiceProvi ...

  6. UltraISO制作U盘启动盘教程

    Step 1: 首先需要安装UltraISO,完成安装后打开软碟通,文件->打开,打开我们的iso镜像 安装包可以百度搜索 Step 2:然后选择我们的U盘 Step 3:然后点击启动-> ...

  7. 运维url收集

    https://www.centos.bz/tag/nagios/ Graphite的百万Metrics实践之路 饿了么 Influxdb 实践之路

  8. 设计能长按并有动画效果且能触发事件的高级view

    设计能长按并有动画效果且能触发事件的高级view 效果图: 源码: LongTapAnimationView.h 与 LongTapAnimationView.m // // LongTapAnima ...

  9. 最优化作业 共轭梯度法 matlab代码

    syms f x1 x2 f=(1/2)*x1^2+x2^2; x=[2;1]; a=[1 0;0 2];% A g1=diff(f,x1); g2=diff(f,x2); g=[g1;g2];%导数 ...

  10. The good life is one inspired by love and guided by knowledge

    The good life is one inspired by love and guided by knowledge 伯特兰·罗素Bertrand Russell18721970 I can a ...