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

Like
chalie001Asked:
Who is Participating?
 
sdstuberCommented:
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

DECLARE
    v_grantee   VARCHAR2(30) := 'HR';  
    v_owner     VARCHAR2(30) := 'HR2';
BEGIN
    FOR x
        IN (SELECT    'grant '
                   || CASE
                          WHEN object_type IN ('TABLE', 'VIEW')
                          THEN
                              'select,insert,update,delete'
                          WHEN object_type IN ('FUNCTION', 'PROCEDURE', 'PACKAGE', 'TYPE')
                          THEN
                              'execute'
                          WHEN object_type = 'SEQUENCE'
                          THEN
                              'select'
                      END
                   || ' on "'
                   || owner
                   || '"."'
                   || object_name
                   || '" to "'
                   || v_grantee
                   || '"'
                       grant_sql
              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'))))
    LOOP
        BEGIN
            DBMS_OUTPUT.put_line(x.grant_sql);

           -- EXECUTE IMMEDIATE x.grant_sql;
        EXCEPTION
            WHEN OTHERS
            THEN
                DBMS_OUTPUT.put_line(SQLERRM);
                DBMS_OUTPUT.put_line(x.grant_sql);
        END;
    END LOOP;
END;

Open in new window

1
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Try:
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.
0
 
QlemoBatchelor, 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 ...
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
chalie001Author Commented:
I must run this in sys user
0
 
QlemoBatchelor, 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?
0
 
chalie001Author Commented:
So must run in it schema
0
 
QlemoBatchelor, 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.
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.