2

In Oracle for IT clients tickets Operation/Requests, we usually get such request to replicate user account with the same privileges which other users have. Or you can say grant my model IDs after this or that user account. so here is simple pseudo code to achieve that.

2 Answers 2

7

In Oracle SQL Developer, open the DBA, Security, and Users section.

Select your account.

Right-click.

Choose, Create Like.

Toggle on 'Copy object privs'

enter image description here

Supply the new username/password.

Voila.

Go to the SQL page to see the code that we've plucked from the data dictionary for you.

It has grabbed quotas, roles, system privs, and object privs.

enter image description here

In version 18.1, you can also just open the User and go to the SQL page and copy/paste/replace the schema name in the code supplied there.

enter image description here

Alas! (I hear you saying, I need the CODE to do this.)

You can see the SQL we're using to get this info from the DB, by opening the Log panel in SQL Developer and clicking on the Statements page.

Here's that code:

select M.NAME,
       decode(
    NVL(
        S.ADMIN_OPTION,
        'NULL'
    ),
    'NO',
    'YES',
    'NULL',
    'NO',
    'YES'
) GRANTED,
       NVL(
    ADMIN_OPTION,
    'NO'
) ADMIN
  from SYSTEM_PRIVILEGE_MAP M,
       (
    select *
      from DBA_SYS_PRIVS
     where (
        GRANTEE   =?
            or ?         = null
    )
) S
 where S.PRIVILEGE (+)   = M.NAME
 order by 1;

select R.ROLE,
       decode(
    S.NAME,
    R.ROLE,
    'YES',
    'NO'
) GRANTED,
       NVL(
    S.ADMIN,
    'NO'
) ADMIN,
       NVL(
    S.DEF,
    'NO'
) DEF
  from DBA_ROLES R,
       (
    select GRANTED_ROLE NAME,
           ADMIN_OPTION ADMIN,
           DEFAULT_ROLE DEF
      from DBA_ROLE_PRIVS
     where GRANTEE   =?
) S
 where S.NAME (+)   = R.ROLE
   and R.AUTHENTICATION_TYPE != 'GLOBAL'
 order by 1;

select OWNER,
       TABLE_NAME,
       PRIVILEGE,
       GRANTABLE
  from DBA_TAB_PRIVS
 where GRANTEE   =?;

select ACCOUNT_STATUS,
       DEFAULT_TABLESPACE DEF,
       TEMPORARY_TABLESPACE TEMP,
       PASSWORD,
       EXTERNAL_NAME,
       EDITIONS_ENABLED,
       ALL_SHARD
  from DBA_USERS
 where USERNAME   =?;

select T.TABLESPACE_NAME,
       NVL(
    Q.UNLIMITED,
    'NO'
) UNLIMITED,
       Q.QUOTA,
       Q.UNIT,
       T.CONTENTS
  from DBA_TABLESPACES T,
       (
    select TABLESPACE_NAME,
           decode(
        MAX_BYTES,
        -1,
        'YES',
        'NO'
    ) UNLIMITED,
           decode(
        MAX_BYTES,
        -1,
        null,
        MAX_BYTES / 1024
    ) QUOTA,
           'K' UNIT
      from DBA_TS_QUOTAS
     where (
        USERNAME   =:1
            or :1         = null
    )
) Q
 where Q.TABLESPACE_NAME (+)   = T.TABLESPACE_NAME
 order by 1;
1
  • 2
    Thank you so much for this! Made it super easy to add a new schema/user.
    – IyaTaisho
    Commented Apr 14, 2020 at 18:08
3

Check & Get the existing user account detail to create a new one to replicate.

select username,default_tablespace,profile from dba_users where username in ('Q861','BX57');
create user BX57 identified by caSrt57#nuj profile USERS; 

Grant privileges to a new user as per existing account with generated pseudo code. You can spool the output to a file and execute separately if the list is too long.

select 'grant '||granted_role||' to BX57;' from DBA_ROLE_PRIVS where grantee = upper('Q861');
select 'grant '||privilege||' to BX57;' from DBA_SYS_PRIVS where grantee in ('Q861');
select 'grant '||privilege||' on '||owner||'.'||table_name||' to BX57;' from DBA_TAB_PRIVS where grantee in ('Q861');
select 'grant '||privilege||' ('||column_name||') '||' on '||owner||'.'||table_name||' to BX57;' from DBA_COL_PRIVS where grantee in ('Q861');

Thank You!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.