sql—如何在oracle中将一个用户复制或克隆到另一个用户

krugob8w  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(352)

我想将所有的权限、角色和命令从一个用户复制到另一个用户。如何在plsql过程或函数中实现这一点。我使用的是oracle12c数据库。

sshcrbum

sshcrbum1#

我使用此脚本生成用户创建:

set long 1000000 pagesize 0 feedback off trimspool on verify off

begin
   dbms_metadata.set_transform_param (dbms_metadata.session_transform, 'SQLTERMINATOR', true);
   dbms_metadata.set_transform_param (dbms_metadata.session_transform, 'PRETTY', true);
end;
/

variable v_username VARCHAR2(30);

exec:v_username := upper('&1');

select dbms_metadata.get_ddl('USER', u.username)
from   dba_users u
where  u.username = :v_username
union all
select dbms_metadata.get_granted_ddl('TABLESPACE_QUOTA', tq.username)
from   dba_ts_quotas tq
where  tq.username = :v_username
and    rownum = 1
union all
select dbms_metadata.get_granted_ddl('ROLE_GRANT', rp.grantee)
from   dba_role_privs rp
where  rp.grantee = :v_username
and    rownum = 1
union all
select dbms_metadata.get_granted_ddl('SYSTEM_GRANT', sp.grantee)
from   dba_sys_privs sp
where  sp.grantee = :v_username
union all
select dbms_metadata.get_granted_ddl('OBJECT_GRANT', tp.grantee)
from   dba_tab_privs tp
where  tp.grantee = :v_username
and    rownum = 1
union all
select dbms_metadata.get_granted_ddl('DEFAULT_ROLE', rp.grantee)
from   dba_role_privs rp
where  rp.grantee = :v_username
and    rp.default_role = 'YES'
and    rownum = 1
union all
select dbms_metadata.get_ddl('PROFILE', u.profile)
from   dba_users u
where  u.username = :v_username
and    u.profile <> 'DEFAULT'
/

相关问题