---
title: "Oracle强制删除指定schema及相关的所有数据库对象"
author: "wanglei8"
date: "2012-05-23T13:24:32Z"
tags: []
categories: ["数据库"]
slug: "oracle-schema"
draft: false
lastmod: "2012-05-23T13:25:33Z"
id: 1538540
url: "https://wanglei8.iteye.com/blog/1538540"
---

创建一个drop_schema.sql文件，内容如下

```sql

set define on
set serveroutput on
connect / as sysdba

define schema = &1
-- Kill Session connecting to the schemas to be dropped
Declare
    Cursor c_session Is
    SELECT s.sid, s.serial#
      FROM gv$session s
      JOIN gv$process p ON p.addr = s.paddr
       AND p.inst_id = s.inst_id
     WHERE s.type != 'BACKGROUND'
       AND s.username = '&schema');
Begin
    For r_session In c_session Loop
        Execute Immediate 'ALTER SYSTEM KILL SESSION '''||r_session.sid||','||r_session.serial#||'''';
    End Loop;
End;
/
-- Drop schemas
Drop User &schema Cascade;

set define off
exit


```


授权

```sql

chmod +x drop_schema.sql

```


进入sqlplus环境，并执行该文件

```sql

$sqlplus /nolog
@drop_schema.sql

```