grant synonym in bulk

hi i have the following use case, i have two schema

hr and hr2


all my database objects are in schema hr2 but developer can only user hr and also the application how can i grant all object ins schema h2 to hr schema i dont what to do one by one i what to do in bulk am in 12c

hr will need to create new object for hr2 procedure function etc so when procedure created it will be like create procedure hr42.procedurename but in application user can call procedure name without schema name but when you select table it wil be hr2.tablename

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
GRANT EXECUTE ON schema1 TO schema

Open in new window

The above it's only to give execute permissions. For tables and views you'll need to GRANT SELECT instead.
There is no bulk grant in oracle.  (i.e.  "GRANT EXECUTE ON schema1 TO schema" will NOT work, nor would "GRANT SELECT" on a schema)

You can fairly easily iterate through objects though and apply grants.

The code below will show you the ddl to do the grants.  Uncomment the execute immediate line in order to actually apply them

    v_grantee   VARCHAR2(30) := 'HR';  
    v_owner     VARCHAR2(30) := 'HR2';
    FOR x
        IN (SELECT    'grant '
                   || CASE
                          WHEN object_type IN ('TABLE', 'VIEW')
                          WHEN object_type IN ('FUNCTION', 'PROCEDURE', 'PACKAGE', 'TYPE')
                          WHEN object_type = 'SEQUENCE'
                   || ' on "'
                   || owner
                   || '"."'
                   || object_name
                   || '" to "'
                   || v_grantee
                   || '"'
              FROM all_objects
             WHERE     owner = v_owner
                   AND object_type IN
                           ('TABLE', 'VIEW', 'FUNCTION', 'PROCEDURE', 'PACKAGE', 'SEQUENCE', 'TYPE')
                   AND (   object_type != 'TABLE'
                        OR (    object_type = 'TABLE'
                            AND object_name NOT IN (SELECT table_name
                                                      FROM all_tables
                                                     WHERE iot_type = 'IOT_OVERFLOW'))))

           -- EXECUTE IMMEDIATE x.grant_sql;
            WHEN OTHERS

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I wonder if it weren't a better idea to use
    alter session CURRENT_SCHEMA = hr
for each session instead of setting up a lot of synonyms, which is a pain to keep control over on metadata change ...
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

chalie001Author Commented:
I must run this in sys user
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
sys has access to all schemas by default. Switching a schema on the fly is easy, as I showed. What is the point?
chalie001Author Commented:
So must run in it schema
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
If that's true, and the code cannot be changed to use hr2 instead of hr (which is poor design), than you have to create synonyms for everything, as shown by sdstuber.
johnsoneSenior Oracle DBACommented:
Even if you change the current schema, you still need to do the grants.

CURRENT_SCHEMA is only used for name resolution, so it can get you around creating synonyms, but it doesn't give you privileges.

If you wanted to create the synonyms, you can easily modify the code that sdstuber posted do that in addition to the grants.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.