如何杀掉一个用户下的所有进程并drop掉这个用户

Copy the sample code below into a file named kill_drop_user.sql.
Open SQL*Plus and connect as user SYS to your database
SQL> CONNECT sys/change_on_install@orcl AS SYSDBA
Create a user called TEST with password TEST
SQL> GRANT connect, resource TO test IDENTIFIED BY test;
Create the procedure kill_drop_user
SQL> @"C:\scripts\kill_drop_user.sql"
Open three (3) SQL*Plus sessions and connect as user TEST.
Execute the PL/SQL script
SQL> SET serveroutput ON size 1000000 SQL> SET timing ON SQL> EXEC kill_drop_user('TEST');
CAUTION

This sample code is provided for educational purposes only, and is not supported by Oracle Support. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
SAMPLE CODE

CREATE OR REPLACE PROCEDURE kill_drop_user (in_username IN VARCHAR2,
                                            sleep_interval IN NUMBER DEFAULT 10)
AS
  PRAGMA AUTONOMOUS_TRANSACTION;

cannot_drop_user EXCEPTION;
  PRAGMA EXCEPTION_INIT(cannot_drop_user, -1940);

user_count    NUMBER := -1;

BEGIN

SELECT count(*) INTO user_count FROM dba_users WHERE username = in_username;
  IF user_count = 0 THEN
    DBMS_OUTPUT.PUT_LINE('User ' || in_username || ' does not exist.');
    RETURN;
  END IF;

FOR i IN (SELECT sid, serial# FROM v$session WHERE username = in_username) LOOP
    EXECUTE IMMEDIATE 'alter system kill session ' || '''' || i.sid || ',' || i.serial# || ''' immediate';
    DBMS_OUTPUT.PUT_LINE('Killing user ' || i.sid || ', ' || i.serial#);
  END LOOP;

LOOP
    BEGIN
      DBMS_OUTPUT.PUT_LINE('Attempting to drop user ' || in_username || '...');
      EXECUTE IMMEDIATE 'DROP USER ' || in_username || ' CASCADE';

EXIT WHEN SQLCODE <> -1940;
    EXCEPTION
      WHEN cannot_drop_user THEN
        --DBMS_OUTPUT.PUT_LINE(SQLERRM);
        DBMS_OUTPUT.PUT_LINE('Waiting ' || sleep_interval || ' seconds for resource clean-up...');
        DBMS_LOCK.SLEEP(sleep_interval);
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('Inner: ' || SQLERRM);
    END;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('Exiting loop with SQLCODE: ' || SQLCODE);
  DBMS_OUTPUT.PUT_LINE('User ' || in_username || ' has been dropped.');

EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Outer: ' || SQLERRM);
END;
/
SAMPLE OUTPUT

SQL*Plus: Release 10.1.0.4.0 - Production on Tue Sep 6 15:34:47 2005
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> CONNECT sys/change_on_install@orcl AS SYSDBA
Connected.
SQL> GRANT connect, resource TO test IDENTIFIED BY test;

Grant succeeded.

SQL> @"C:\scripts\kill_drop_user.sql"
Procedure created.

SQL> SET serveroutput ON size 1000000

SQL> SET timing ON

SQL> EXEC kill_drop_user('TEST');
Killing user 152, 6915
Killing user 153, 326
Killing user 154, 156
Attempting to drop user TEST...
Waiting 10 seconds for resource clean-up...
Attempting to drop user TEST...
Exiting loop with SQLCODE: 0
User TEST has been dropped.

PL/SQL procedure successfully completed.

Elapsed: 00:00:10.71

SQL> -- verify user has been dropped
SQL> CONNECT test/test@orcl
ERROR:
ORA-01017: invalid username/password; logon denied

Warning: You are no longer connected to ORACLE.
SQL>

转自MOS
如何杀掉一个用户下的所有进程并drop掉这个用户

Copy the sample code below into a file named kill_drop_user.sql.
Open SQL*Plus and connect as user SYS to your database
SQL> CONNECT sys/change_on_install@orcl AS SYSDBA
Create a user called TEST with password TEST
SQL> GRANT connect, resource TO test IDENTIFIED BY test;
Create the procedure kill_drop_user
SQL> @"C:\scripts\kill_drop_user.sql"
Open three (3) SQL*Plus sessions and connect as user TEST.
Execute the PL/SQL script
SQL> SET serveroutput ON size 1000000 SQL> SET timing ON SQL> EXEC kill_drop_user('TEST');
CAUTION

This sample code is provided for educational purposes only, and is not supported by Oracle Support. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
SAMPLE CODE

CREATE OR REPLACE PROCEDURE kill_drop_user (in_username IN VARCHAR2,
                                            sleep_interval IN NUMBER DEFAULT 10)
AS
  PRAGMA AUTONOMOUS_TRANSACTION;

cannot_drop_user EXCEPTION;
  PRAGMA EXCEPTION_INIT(cannot_drop_user, -1940);

user_count    NUMBER := -1;

BEGIN

SELECT count(*) INTO user_count FROM dba_users WHERE username = in_username;
  IF user_count = 0 THEN
    DBMS_OUTPUT.PUT_LINE('User ' || in_username || ' does not exist.');
    RETURN;
  END IF;

FOR i IN (SELECT sid, serial# FROM v$session WHERE username = in_username) LOOP
    EXECUTE IMMEDIATE 'alter system kill session ' || '''' || i.sid || ',' || i.serial# || ''' immediate';
    DBMS_OUTPUT.PUT_LINE('Killing user ' || i.sid || ', ' || i.serial#);
  END LOOP;

LOOP
    BEGIN
      DBMS_OUTPUT.PUT_LINE('Attempting to drop user ' || in_username || '...');
      EXECUTE IMMEDIATE 'DROP USER ' || in_username || ' CASCADE';

EXIT WHEN SQLCODE <> -1940;
    EXCEPTION
      WHEN cannot_drop_user THEN
        --DBMS_OUTPUT.PUT_LINE(SQLERRM);
        DBMS_OUTPUT.PUT_LINE('Waiting ' || sleep_interval || ' seconds for resource clean-up...');
        DBMS_LOCK.SLEEP(sleep_interval);
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('Inner: ' || SQLERRM);
    END;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('Exiting loop with SQLCODE: ' || SQLCODE);
  DBMS_OUTPUT.PUT_LINE('User ' || in_username || ' has been dropped.');

EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Outer: ' || SQLERRM);
END;
/
SAMPLE OUTPUT

SQL*Plus: Release 10.1.0.4.0 - Production on Tue Sep 6 15:34:47 2005
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> CONNECT sys/change_on_install@orcl AS SYSDBA
Connected.
SQL> GRANT connect, resource TO test IDENTIFIED BY test;

Grant succeeded.

SQL> @"C:\scripts\kill_drop_user.sql"
Procedure created.

SQL> SET serveroutput ON size 1000000

SQL> SET timing ON

SQL> EXEC kill_drop_user('TEST');
Killing user 152, 6915
Killing user 153, 326
Killing user 154, 156
Attempting to drop user TEST...
Waiting 10 seconds for resource clean-up...
Attempting to drop user TEST...
Exiting loop with SQLCODE: 0
User TEST has been dropped.

PL/SQL procedure successfully completed.

Elapsed: 00:00:10.71

SQL> -- verify user has been dropped
SQL> CONNECT test/test@orcl
ERROR:
ORA-01017: invalid username/password; logon denied

Warning: You are no longer connected to ORACLE.
SQL>

转自MOS

Copy the sample code below into a file named kill_drop_user.sql.
Open SQL*Plus and connect as user SYS to your database
SQL> CONNECT sys/change_on_install@orcl AS SYSDBA
Create a user called TEST with password TEST
SQL> GRANT connect, resource TO test IDENTIFIED BY test;
Create the procedure kill_drop_user
SQL> @"C:\scripts\kill_drop_user.sql"
Open three (3) SQL*Plus sessions and connect as user TEST.
Execute the PL/SQL script
SQL> SET serveroutput ON size 1000000 SQL> SET timing ON SQL> EXEC kill_drop_user('TEST');
CAUTION

This sample code is provided for educational purposes only, and is not supported by Oracle Support. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
SAMPLE CODE

CREATE OR REPLACE PROCEDURE kill_drop_user (in_username IN VARCHAR2,
                                            sleep_interval IN NUMBER DEFAULT 10)
AS
  PRAGMA AUTONOMOUS_TRANSACTION;

cannot_drop_user EXCEPTION;
  PRAGMA EXCEPTION_INIT(cannot_drop_user, -1940);

user_count    NUMBER := -1;

BEGIN

SELECT count(*) INTO user_count FROM dba_users WHERE username = in_username;
  IF user_count = 0 THEN
    DBMS_OUTPUT.PUT_LINE('User ' || in_username || ' does not exist.');
    RETURN;
  END IF;

FOR i IN (SELECT sid, serial# FROM v$session WHERE username = in_username) LOOP
    EXECUTE IMMEDIATE 'alter system kill session ' || '''' || i.sid || ',' || i.serial# || ''' immediate';
    DBMS_OUTPUT.PUT_LINE('Killing user ' || i.sid || ', ' || i.serial#);
  END LOOP;

LOOP
    BEGIN
      DBMS_OUTPUT.PUT_LINE('Attempting to drop user ' || in_username || '...');
      EXECUTE IMMEDIATE 'DROP USER ' || in_username || ' CASCADE';

EXIT WHEN SQLCODE <> -1940;
    EXCEPTION
      WHEN cannot_drop_user THEN
        --DBMS_OUTPUT.PUT_LINE(SQLERRM);
        DBMS_OUTPUT.PUT_LINE('Waiting ' || sleep_interval || ' seconds for resource clean-up...');
        DBMS_LOCK.SLEEP(sleep_interval);
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('Inner: ' || SQLERRM);
    END;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('Exiting loop with SQLCODE: ' || SQLCODE);
  DBMS_OUTPUT.PUT_LINE('User ' || in_username || ' has been dropped.');

EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Outer: ' || SQLERRM);
END;
/
SAMPLE OUTPUT

SQL*Plus: Release 10.1.0.4.0 - Production on Tue Sep 6 15:34:47 2005
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> CONNECT sys/change_on_install@orcl AS SYSDBA
Connected.
SQL> GRANT connect, resource TO test IDENTIFIED BY test;

Grant succeeded.

SQL> @"C:\scripts\kill_drop_user.sql"
Procedure created.

SQL> SET serveroutput ON size 1000000

SQL> SET timing ON

SQL> EXEC kill_drop_user('TEST');
Killing user 152, 6915
Killing user 153, 326
Killing user 154, 156
Attempting to drop user TEST...
Waiting 10 seconds for resource clean-up...
Attempting to drop user TEST...
Exiting loop with SQLCODE: 0
User TEST has been dropped.

PL/SQL procedure successfully completed.

Elapsed: 00:00:10.71

SQL> -- verify user has been dropped
SQL> CONNECT test/test@orcl
ERROR:
ORA-01017: invalid username/password; logon denied

Warning: You are no longer connected to ORACLE.
SQL>

转自MOS

如何杀掉一个用户下的所有进程并drop掉这个用户的更多相关文章

  1. 【linux】kill ;杀死某一用户下的所有进程

    [linux]kill :杀死某一用户下的所有进程 https://my.oschina.net/u/347414/blog/600854

  2. linux暂停一个在运行中的进程【转】

    转自:https://blog.csdn.net/Tim_phper/article/details/53536621 转载于: http://www.cszhi.com/20120328/linux ...

  3. linux下批量kill进程的方法

    --kill某个用户下的所有进程(用户为test)--pkill  # pkill -u test--killall  # killall -u test--ps  # ps -ef | grep t ...

  4. (转)Linux下Oracle启动、建立表空间、用户、授权、数据库导入导出

    Linux系列 启动1.启动数据库实例,分为两步:第一步,启动监听:第二步,启动数据库实例. 1.1进入到sqlplus启动实例 [oracle@redhat ~]$ su - oracle      ...

  5. 使用sys用户创建其他用户下的dblink

    因为dblink的创建和删除只能是它的所属用户来操作,所以我们无法直接使用sys用户创建其他用户下的dblink,当遇到有这样的需求时,可以先建立该用户下存储过程,再通过调用这个存储过程来间接实现. ...

  6. spool命令、创建一个表,创建而且copy表,查看别的用户下的表,rowid行地址 索引的时候使用,表的增删改查,删除表,oracle的回收站

      1.spool命令 spool "D:\test.txt" spool off SQL> host cls 2.创建一个表 SQL> --条件(1):有创建 ...

  7. oracle impdp将导出用户的所有对象导入至另一个用户下,生成的触发器语句问题处理

    问题产生的操作步骤及详细说明: 1)操作的数据库是oracle 11g,先通过命令将用户GAS_NEW的数据导出,命令语句如下: expdp GAS_NEW/GAS_NEW@ORCL schemas= ...

  8. Oracle 如何删除掉一个用户下的所有对象

    create or replace procedure drop_all as cursor cur_obj is select uo.OBJECT_NAME, uo.OBJECT_TYPE from ...

  9. linux下的守护进程

    关于守护进程,在此会介绍一下几种: 1.screen 2.supervisord(python) 一:Screen 开始使用Screen 简单来说,Screen是一个可以在多个进程之间多路复用一个物理 ...

随机推荐

  1. 可持久化Treap 赛前摸鱼笔记

    1.基本结构 随机化工具 unsigned int SEED = 19260817; //+1s inline int Rand(){ SEED=SEED*1103515245+12345; retu ...

  2. jQuery序列化表单 serialize() serializeArray()

    1.serialize()方法 描述:序列化表单内容为字符串,用于Ajax请求. 格式:var data = $(form).serialize(); 2.serializeArray()方法 描述: ...

  3. [转] 用Python建立最简单的web服务器

    [From] http://www.cnblogs.com/xuxn/archive/2011/02/14/build-simple-web-server-with-python.html 利用Pyt ...

  4. js中点和向量的基本方法

    var Point=function(x,y){ this.x= Number(x.toFixed(2))||0; this.y=Number(y.toFixed(2))||0; } Point.pr ...

  5. 基于scrapy的一些实例

    一.爬取斗鱼主播 1. 爬虫文件 # -*- coding: utf-8 -*- import scrapy import json from Douyu.items import DouyuItem ...

  6. jquery将日期转换成指定格式的字符串

    引用jquery文件,如<script type="text/javascript" src="jquery-1.8.3.min.js"></ ...

  7. oracle 备份恢复篇(六)---基于12c的pdb备份与恢复

    一,备份前提描述 SQL> show con_name CON_NAME ------------------------------ CDB$ROOT SQL> archive log ...

  8. oracle 笔记---(六)__表空间

    查看表空间的大小 select tablespace_name,block_size,contents from dba_tablespaces; 查看表空间对应的数据文件 select file_n ...

  9. linux 下安装 mysql 并配置 python 开发环境

    1.安装 mysql ,安装过程中将提示设置 root 用户的密码,默认可以设置为 rootadmin . $ sudo apt-get install mysql-server 2.安装 mysql ...

  10. js中函数带不带var的本质区别是什么

    本质区别是:带var的是定义,属于statement:不带var的是赋值,属于expression.不带var时,解释器认为变量已经定义过了,会在函数中找相应的定义,如果找不到,就会认为变量是在外一层 ...