文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-04-02 19:55

关注
如何杀掉一个用户下的所有进程并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
阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯