Advertisement
colemar

How to extract user details in Oracle

May 16th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Usage: @ get_user_ddl username
  2.  
  3. SET pagesize 0 linesize 999 LONG 9999 longchunksize 9999 verify OFF feedback OFF
  4.  
  5. COLUMN MYUSER ON NEW_VALUE MYUSER NOPRINT
  6. UNDEFINE MYUSER
  7.  
  8. SELECT USERNAME AS MYUSER FROM DBA_USERS WHERE USERNAME = UPPER('&1');
  9.  
  10. prompt -- create profile
  11. SELECT
  12.   CASE PROFILE
  13.     WHEN 'DEFAULT'
  14.     THEN '/* user &MYUSER has DEFAULT profile */'
  15.     ELSE CAST( dbms_metadata.get_ddl( 'PROFILE', PROFILE ) || '/' AS VARCHAR2(4000) )
  16.   END
  17. FROM DBA_USERS
  18. WHERE USERNAME = '&MYUSER';
  19.  
  20. prompt -- create user
  21. SELECT dbms_metadata.get_ddl( 'USER', '&MYUSER' ) || '/' FROM DUAL;
  22.  
  23. prompt -- create roles
  24. SELECT
  25.   REGEXP_REPLACE( dbms_metadata.get_ddl( 'ROLE', GRANTED_ROLE ), '('||CHR(10)||' *){2}|\s+$', ';\1' )
  26. FROM DBA_ROLE_PRIVS
  27. WHERE GRANTEE = '&MYUSER';
  28.  
  29. prompt -- grant roles to roles
  30. SELECT
  31.   REGEXP_REPLACE( dbms_metadata.get_granted_ddl( 'ROLE_GRANT', GRANTED_ROLE ), '('||CHR(10)||' *){2}|\s+$', ';\1' )
  32. FROM DBA_ROLE_PRIVS a
  33. WHERE GRANTEE = '&MYUSER'
  34.   AND EXISTS (SELECT * FROM DBA_ROLE_PRIVS WHERE GRANTEE = a.GRANTED_ROLE);
  35.  
  36. prompt -- grant system privileges to roles
  37. SELECT
  38.   REGEXP_REPLACE( dbms_metadata.get_granted_ddl( 'SYSTEM_GRANT', GRANTED_ROLE ), '('||CHR(10)||' *){2}|\s+$', ';\1' )
  39. FROM DBA_ROLE_PRIVS a
  40. WHERE GRANTEE = '&MYUSER'
  41.   AND EXISTS (SELECT * FROM DBA_SYS_PRIVS WHERE GRANTEE = a.GRANTED_ROLE);
  42.  
  43. prompt -- grant object privileges to roles
  44. SELECT
  45.   REGEXP_REPLACE( dbms_metadata.get_granted_ddl( 'OBJECT_GRANT', GRANTED_ROLE ), '('||CHR(10)||' *){2}|\s+$', ';\1' )
  46. FROM DBA_ROLE_PRIVS a
  47. WHERE GRANTEE = '&MYUSER'
  48.   AND EXISTS (SELECT * FROM ROLE_TAB_PRIVS WHERE ROLE = a.GRANTED_ROLE);
  49.  
  50. prompt -- grant roles
  51. SELECT
  52.   REGEXP_REPLACE( dbms_metadata.get_granted_ddl( 'ROLE_GRANT', GRANTEE ), '('||CHR(10)||' *){2}|\s+$', ';\1' )
  53. FROM DBA_ROLE_PRIVS
  54. WHERE GRANTEE = '&MYUSER'
  55.   AND ROWNUM <= 1;
  56.  
  57. prompt -- assign default roles
  58. SELECT dbms_metadata.get_granted_ddl( 'DEFAULT_ROLE', '&MYUSER' ) || '/' FROM DUAL;
  59.  
  60. prompt -- grant system privileges
  61. SELECT
  62.   REGEXP_REPLACE( dbms_metadata.get_granted_ddl( 'SYSTEM_GRANT', GRANTEE ), '('||CHR(10)||' *){2}|\s+$', ';\1' )
  63. FROM DBA_SYS_PRIVS
  64. WHERE GRANTEE = '&MYUSER'
  65.   AND ROWNUM <= 1;
  66.  
  67. prompt -- grant object privileges
  68. SELECT
  69.   REGEXP_REPLACE( dbms_metadata.get_granted_ddl( 'OBJECT_GRANT', GRANTEE ), '('||CHR(10)||' *){2}|\s+$', ';\1' )
  70. FROM DBA_TAB_PRIVS
  71. WHERE GRANTEE = '&MYUSER'
  72.   AND ROWNUM <= 1;
  73.  
  74. prompt -- assign tablespace quotas
  75. SELECT
  76.   dbms_metadata.get_granted_ddl( 'TABLESPACE_QUOTA', USERNAME ) || '/'
  77. FROM DBA_TS_QUOTAS
  78. WHERE USERNAME = '&MYUSER'
  79.   AND ROWNUM <= 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement