SQL> r
1 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 username='SCOTT';#定义cursor
1 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 username='SCOTT';#定义cursor
5 begin
6 open cur_session;#打开cusor
6 open cur_session;#打开cusor
7 loop #打开游标马上开始循环,因为cursor是一条条取数据的
8 fetch cur_session into v_sid,v_serial#;#把游标的数据放入上面定义的type变量中
8 fetch cur_session into v_sid,v_serial#;#把游标的数据放入上面定义的type变量中
#根据以上的type变量及游标生成批量杀session的动态sql脚本,并执行
9 execute immediate 'alter system kill session '''||v_sid||','||v_serial#||'''';
10 exit when cur_session%notfound;#要加个异常处理,不然永远是死循环
11 dbms_output.put_line('cursor date have been fetched ending');
12 end loop;#loop也有成双匹配出现
10 exit when cur_session%notfound;#要加个异常处理,不然永远是死循环
11 dbms_output.put_line('cursor date have been fetched ending');
12 end loop;#loop也有成双匹配出现
13 close cur_session;#游标处理完后,关闭游标
14* end;
PL/SQL procedure successfully completed.
SQL> /
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9240380/viewspace-666622/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/9240380/viewspace-666622/