oracle_脚本获取创建用户语句

在某些情况下,我们需要获得数据库用户的创建脚本(包含系统权限,对象权限,配额等相关语句),这些东西如果人工去做绝对是体力活,在asktom网站上看到相关脚本,做了测试和验证,确实很好
-----------------------------------------
-- ###########################################
---------------------------------------------
create or replace procedure GET_CREATE_USER_DDL
  as
cursor get_username is
select username
from dba_users
--where username IN('CHF','XIFENFEI')
;
 
begin
for l_user in get_username loop
 
DBMS_OUTPUT.PUT_LINE('-----------------------');
DBMS_OUTPUT.PUT_LINE('select (case');
DBMS_OUTPUT.PUT_LINE('         when ((selectcount(*)');
DBMS_OUTPUT.PUT_LINE('               from   dba_users');
DBMS_OUTPUT.PUT_LINE('               where  username = '''||l_user.username||''') >0)');
DBMS_OUTPUT.PUT_LINE('         then  dbms_metadata.get_ddl (''USER'','''||l_user.username||''')');
DBMS_OUTPUT.PUT_LINE('         else   to_clob(''   -- Note: User not found!'')');
DBMS_OUTPUT.PUT_LINE('         end ) "--Extracted_DDL" fromdual');
DBMS_OUTPUT.PUT_LINE('UNION ALL');
 
DBMS_OUTPUT.PUT_LINE('-----------------------');
DBMS_OUTPUT.PUT_LINE('select (case');
DBMS_OUTPUT.PUT_LINE('         when ((selectcount(*)');
DBMS_OUTPUT.PUT_LINE('               from   dba_ts_quotas');
DBMS_OUTPUT.PUT_LINE('               where  username = '''||l_user.username||''') >0)');
DBMS_OUTPUT.PUT_LINE('         then  dbms_metadata.get_granted_ddl(''TABLESPACE_QUOTA'',
'''||l_user.username||''')');
DBMS_OUTPUT.PUT_LINE('         else   to_clob(''   -- Note: No TS Quotas found!'')');
DBMS_OUTPUT.PUT_LINE('         end )   fromdual');
DBMS_OUTPUT.PUT_LINE('UNION ALL');
 
DBMS_OUTPUT.PUT_LINE('-----------------------');
DBMS_OUTPUT.PUT_LINE('select (case');
DBMS_OUTPUT.PUT_LINE('         when ((selectcount(*)');
DBMS_OUTPUT.PUT_LINE('               from   dba_role_privs');
DBMS_OUTPUT.PUT_LINE('               where  grantee = '''||l_user.username||''') >0)');
DBMS_OUTPUT.PUT_LINE('         then  dbms_metadata.get_granted_ddl(''ROLE_GRANT'',
'''||l_user.username||''')');
DBMS_OUTPUT.PUT_LINE('         else   to_clob(''   -- Note: No granted roles found!'')');
DBMS_OUTPUT.PUT_LINE('         end ) from dual');
DBMS_OUTPUT.PUT_LINE('UNION ALL');
 
DBMS_OUTPUT.PUT_LINE('-----------------------');
DBMS_OUTPUT.PUT_LINE('select (case');
DBMS_OUTPUT.PUT_LINE('         when ((selectcount(*)');
DBMS_OUTPUT.PUT_LINE('               from   dba_sys_privs');
DBMS_OUTPUT.PUT_LINE('               where  grantee = '''||l_user.username||''') >0)');
DBMS_OUTPUT.PUT_LINE('         then  dbms_metadata.get_granted_ddl(''SYSTEM_GRANT'',
'''||l_user.username||''')');
DBMS_OUTPUT.PUT_LINE('         else   to_clob(''   -- Note: No System Privilegesfound!'')');
DBMS_OUTPUT.PUT_LINE('         end ) from dual');
DBMS_OUTPUT.PUT_LINE('UNION ALL');
 
DBMS_OUTPUT.PUT_LINE('-----------------------');
DBMS_OUTPUT.PUT_LINE('select (case');
DBMS_OUTPUT.PUT_LINE('         when ((selectcount(*)');
DBMS_OUTPUT.PUT_LINE('               from   dba_tab_privs');
DBMS_OUTPUT.PUT_LINE('               where  grantee = '''||l_user.username||''') >0)');
DBMS_OUTPUT.PUT_LINE('         then  dbms_metadata.get_granted_ddl(''OBJECT_GRANT'',
'''||l_user.username||''')');
DBMS_OUTPUT.PUT_LINE('         else   to_clob(''   -- Note: No Object Privilegesfound!'')');
DBMS_OUTPUT.PUT_LINE('         end ) from dual');
DBMS_OUTPUT.PUT_LINE('/');
DBMS_OUTPUT.PUT_LINE('----------------------------------------------------------------------');
 
end loop;
end;
/
 
---------------------------------------------
--###########################################
---------------------------------------------
The above proc when called with the foll. will give the SQLsfor all users:
 
---------------------------------------------
-- ###########################################
---------------------------------------------
set pages 50000
set serveroutput on size unlimited
spool /tmp/exec_GET_CREATE_USER_DDL.sql
exec GET_CREATE_USER_DDL
spool off
---------------------------------------------
-- ###########################################
---------------------------------------------
 
These SQLs generated can in turn be run as follows to get themaster-list of all the grants in the database:
 
---------------------------------------------
-- ###########################################
---------------------------------------------
spool /tmp/GET_CREATE_USER_DDL.sql
conn / as sysdba
set long 1000000000
set pages 50000
execDBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR',TRUE);
@/tmp/exec_GET_CREATE_USER_DDL.sql
spool off
---------------------------------------------
-- ###########################################
---------------------------------------------
通过这个脚本,我们可以在游标地方限制我们需要获得脚本的用户,而最终得到的/tmp/GET_CREATE_USER_DDL.sql就是我们需要的创建用户的对应脚本.
如果只需要获得一个用户创建相关脚本,只需要执行如下sql即可
execDBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR',TRUE);
select (case
        when ((select count(*)
                from   dba_users
                where  username = '&&Username') > 0)
        then   dbms_metadata.get_ddl('USER', '&&Username')
        else   to_clob ('  -- Note: User not found!')
        end ) Extracted_DDL from dual
UNION ALL
select (case
        when ((select count(*)
                from   dba_ts_quotas
                where  username = '&&Username') > 0)
        then  dbms_metadata.get_granted_ddl('TABLESPACE_QUOTA', '&&Username')
        else   to_clob ('  -- Note: No TS Quotas found!')
        end ) from dual
UNION ALL
select (case
        when ((select count(*)
                from   dba_role_privs
                where  grantee = '&&Username') > 0)
        then  dbms_metadata.get_granted_ddl ('ROLE_GRANT','&&Username')
        else   to_clob ('  -- Note: No granted Roles found!')
        end ) from dual
UNION ALL
select (case
        when ((select count(*)
                from   dba_sys_privs
                where  grantee = '&&Username') > 0)
        then  dbms_metadata.get_granted_ddl ('SYSTEM_GRANT','&&Username')
        else   to_clob ('  -- Note: No System Privileges found!')
        end ) from dual
UNION ALL
select (case
        when ((select count(*)
                from   dba_tab_privs
                where  grantee = '&&Username') > 0)
        then  dbms_metadata.get_granted_ddl ('OBJECT_GRANT','&&Username')
        else   to_clob ('  -- Note: No Object Privileges found!')
        end ) from dual
/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
shell脚本是一种命令语言,可以通过编写一系列命令来实现自动化任务。下面是一个示例的shell脚本,用于创建或删除Oracle用户和表空间。 1. 创建Oracle用户和表空间的脚本: ```shell #!/bin/bash # 设置Oracle环境变量 export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1 export ORACLE_SID=orcl # 创建Oracle用户 function create_user() { sqlplus -s username/password@localhost <<EOF CREATE USER myuser IDENTIFIED BY mypassword; GRANT CONNECT, RESOURCE TO myuser; EXIT; EOF } # 创建Oracle表空间 function create_tablespace() { sqlplus -s username/password@localhost <<EOF CREATE TABLESPACE mytablespace DATAFILE '/u01/app/oracle/oradata/orcl/mytablespace.dbf' SIZE 100M AUTOEXTEND ON NEXT 10M MAXSIZE 500M; ALTER USER myuser DEFAULT TABLESPACE mytablespace; EXIT; EOF } # 调用函数来创建用户和表空间 create_user create_tablespace ``` 上述脚本首先设置Oracle环境变量,然后定义了两个函数create_user和create_tablespace,分别用于创建Oracle用户和表空间。在函数内部使用sqlplus命令连接到Oracle数据库,并执行相应的SQL语句来实现创建用户和表空间的操作。 2. 删除Oracle用户和表空间的脚本: ```shell #!/bin/bash # 设置Oracle环境变量 export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1 export ORACLE_SID=orcl # 删除Oracle用户 function delete_user() { sqlplus -s username/password@localhost <<EOF DROP USER myuser CASCADE; EXIT; EOF } # 删除Oracle表空间 function delete_tablespace() { sqlplus -s username/password@localhost <<EOF DROP TABLESPACE mytablespace INCLUDING CONTENTS AND DATAFILES; EXIT; EOF } # 调用函数来删除用户和表空间 delete_user delete_tablespace ``` 上述脚本创建脚本类似,只是SQL语句改为删除用户和表空间的操作。通过调用相应的函数来实现删除用户和表空间的功能。 需要注意的是,以上脚本中的username/password@localhost需要根据实际情况进行修改,以正确连接到Oracle数据库。另外,创建表空间时指定的文件路径和名称也需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值