
BEGIN

   DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR', true);

   DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'PRETTY',true);

END;

/
 
set serveroutput on heading off feedback off define off echo off flush off pagesize 0 linesize 9999 long 99999 LONGCHUNKSIZE 99999 TRIMSPOOL ON;
 
spool sourcedb_roles.sql
 
select 'spool sourcedb_roles.log' from dual;

select 'create role ' || role || ';' from dba_roles where oracle_maintained = 'N' and authentication_type <> 'GLOBAL';

select 'spool off;' from dual;

spool sourcedb_globalroles.sql

select 'spool sourcedb_globalroles.log' from dual;

select 'create role '||role || ' identified globally as ''' || external_name || ''';' from dba_roles where oracle_maintained = 'N' and authentication_type = 'GLOBAL' and role NOT LIKE 'SBI%' order by role;

select 'spool off;' from dual;
 
spool sourcedb_grants_1.sql
 
select 'spool sourcedb_grants_1.log' from dual;

select 'grant ' || granted_role || ' to ' || grantee || ';' from sys.dba_role_privs
where grantee in (select username from dba_users where oracle_maintained = 'N' and profile like 'APPLICATION%' and username not like 'C##%' and default_tablespace not like 'SYS%' and username <> 'PDBADMIN');
 
select 'grant ' || privilege || ' to ' || grantee || ';' from sys.dba_sys_privs
where grantee in 
(select username grantee from dba_users where oracle_maintained = 'N' and profile like 'APPLICATION%' and username not like 'C##%' and default_tablespace not like 'SYS%' and username <> 'PDBADMIN'
union 
select ROLE grantee from dba_roles where oracle_maintained = 'N' and inherited = 'NO');
 
 
spool sourcedb_grants_2.sql
 
select 'spool sourcedb_grants_2.log' from dual;

select 'grant ' || privilege || ' on ' || owner || '.' || table_name || ' to ' || grantee || case when grantable = 'YES' then ' with grant option' end || ';' from sys.dba_tab_privs
where grantee in (
select username grantee from dba_users where oracle_maintained = 'N' and profile like 'APPLICATION%' and username not like 'C##%' and default_tablespace not like 'SYS%' and username <> 'PDBADMIN'
union 
select ROLE grantee from dba_roles where oracle_maintained = 'N' and inherited = 'NO');

select 'spool off;' from dual;
 
spool sourcedb_grants_3.sql
 
select 'spool sourcedb_grants_3.log' from dual;

select 'grant ' || granted_role || ' to ' || grantee || ';' from dba_role_privs where grantee  in (select role from dba_roles where oracle_maintained = 'N');

select 'spool off;' from dual;
 
spool off;
 