DB Migration Query

 -- User and Grant DDL


SELECT DBMS_METADATA.GET_DDL('USER',username) as script from DBA_USERS where username='&username';


SELECT DBMS_METADATA.GET_GRANTED_DDL('SYSTEM_GRANT',grantee)as script from DBA_SYS_PRIVS where grantee='&username' and rownum=1;


SELECT DBMS_METADATA.GET_GRANTED_DDL('ROLE_GRANT',grantee)as script from DBA_ROLE_PRIVS where grantee='&username' and rownum=1;


SELECT DBMS_METADATA.GET_GRANTED_DDL('OBJECT_GRANT',grantee)as script from DBA_TAB_PRIVS where grantee='&username' and rownum=1;


SELECT DBMS_METADATA.GET_DDL('USERNAME','default_tablespace') as script from DBA_USERS where username='&username';

-- Role and Grant DDL


select dbms_metadata.get_ddl('ROLE', r.role) AS ddl

from   dba_roles r

where  r.role = :v_role

union all

select dbms_metadata.get_granted_ddl('ROLE_GRANT', rp.grantee) AS ddl

from   dba_role_privs rp

where  rp.grantee = :v_role

and    rownum = 1

union all

select dbms_metadata.get_granted_ddl('SYSTEM_GRANT', sp.grantee) AS ddl

from   dba_sys_privs sp

where  sp.grantee = :v_role

and    rownum = 1

union all

select dbms_metadata.get_granted_ddl('OBJECT_GRANT', tp.grantee) AS ddl

from   dba_tab_privs tp

where  tp.grantee = :v_role

and    rownum = 1

/



-- Tablepace DDL


select dbms_metadata.get_ddl('TABLESPACE',tb.tablespace_name)||';' as table_space from dba_tablespaces tb

where tb.tablespace_name not in ('SYSTEM','SYSAUX','USERS','UNDOTBS1');


-- Here we are just considering the tablespace names and creating them on target RDS with below query.

select tablespace_name from dba_tablespaces where tablespace not in ('SYSTEM','SYSAUX','USERS','UNDOTBS1');


create tablespace tablespace_name;



-- Profile DDL

select dbms_metadata.get_ddl('PROFILE', profile) as profile_ddl

from (select distinct profile from dba_profiles);



--Generate Foreign Key Constraints DDL


SELECT DBMS_METADATA.GET_DEPENDENT_DDL('REF_CONSTRAINT','TABLE_NAME','OWNER') from dual;



--DB Links DDL


Set long 1000

SELECT DBMS_METADATA.GET_DDL('DB_LINK',db.db_link,db.owner) from dba_db_links db;

====================

 SET SERVEROUTPUT ON;

DECLARE

val NUMBER;

BEGIN

FOR R IN (SELECT OWNER, TABLE_NAME FROM DBA_TABLES WHERE OWNER='MANOJ'  ORDER BY TABLE_NAME) LOOP

EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || R.OWNER || '.' || R.TABLE_NAME INTO val;

DBMS_OUTPUT.PUT_LINE(R.TABLE_NAME || ',' || val );

END LOOP;

END;


===================



Comments

Popular Posts