Oracle: copy a role from one database to another?

18,527

DBMS_METADATA might be helpful: http://www.orafaq.com/wiki/DBMS_METADATA

SELECT dbms_metadata.get_ddl('ROLE', role) FROM dba_roles;
SELECT dbms_metadata.get_granted_ddl('ROLE_GRANT',  '&&your_role_name') FROM dual;
SELECT dbms_metadata.get_granted_ddl('SYSTEM_GRANT','&&your_role_name') FROM dual;
SELECT dbms_metadata.get_granted_ddl('OBJECT_GRANT','&&your_role_name') FROM dual;
Share:
18,527
Mark Harrison
Author by

Mark Harrison

I'm a Software Engineer at Google where I work on machine learning planning systems. From 2001-2015 I was the Pixar Tech Lead of the Data Management Group. My 50-year charter was to store and catalog all data and metadata related to the Studio's feature films. This system ("Templar") is in use to this day. From 1997 to 2001 I lived in Beijing, China and was the Chief Software Architect at AsiaInfo, the company that built China's Internet. While there my software was used to grow the China Internet from 200K to 65M users. The last I heard they were at 350M+ users. I studied computer science and worked in Texas for many years. I wrote a couple of computer books... the best one was in print for 20 years. Feel free to drop me a line! [email protected]

Updated on June 14, 2022

Comments

  • Mark Harrison
    Mark Harrison almost 2 years

    I would like to duplicate some roles from a production database to a test database.

    How can I programatically generate a text-based SQLPLUS script to do this? Since I'm only interested in the roles I don't want to use EXP/IMP.

  • Mark Harrison
    Mark Harrison about 12 years
    It's useful, but I'd prefer not to roll my own, but instead use something which has been tested and debugged. Thanks!
  • Jens Schauder
    Jens Schauder about 12 years
    This is hardly rolling your own. How do you think you will find stuff that is better tested then the stuff that comes with your database?
  • Mark Harrison
    Mark Harrison about 12 years
    I'm not sure which pieces of metadata to retrieve in order to fully replicate a role, so I was hoping somebody might have a script that would do this. Will the last item (with the four selects) give me the complete set of information to replicate the role?
  • Jens Schauder
    Jens Schauder about 12 years
    the package produces DDL statements.
  • Mark Harrison
    Mark Harrison about 12 years
    great... and are the four statements enough to completely reproduce the role?
  • Jens Schauder
    Jens Schauder about 12 years
    Sorry don't know the details. You'll have to experiment or read the documentation.
  • Mark Harrison
    Mark Harrison about 12 years
    Docs don't say, as far as I can tell, but experimentation shows that it should be the right thing. I'll edit your answer to include the lines and accept. many thanks!!
  • Ben
    Ben about 12 years