7

Is there a way to grant all privileges to a user on Oracle schema? I tried the following command but it only grants permission on specific tables in a schema. What I want is to give this user all permissions on a given schema.

GRANT ALL ON MyTable TO MyUser;
Frank Martin
  • 3,147
  • 16
  • 52
  • 73

3 Answers3

12

You can do it in a loop and grant by dynamic SQL:

BEGIN
   FOR objects IN
   (
         SELECT 'GRANT ALL ON "'||owner||'"."'||object_name||'" TO MyUser' grantSQL
           FROM all_objects
          WHERE owner = 'MY_SCHEMA'
            AND object_type NOT IN
                (
                   --Ungrantable objects.  Your schema may have more.
                   'SYNONYM', 'INDEX', 'INDEX PARTITION', 'DATABASE LINK',
                   'LOB', 'TABLE PARTITION', 'TRIGGER'
                )
       ORDER BY object_type, object_name
   ) LOOP
      BEGIN
         EXECUTE IMMEDIATE objects.grantSQL;
      EXCEPTION WHEN OTHERS THEN
         --Ignore ORA-04063: view "X.Y" has errors.
         --(You could potentially workaround this by creating an empty view,
         -- granting access to it, and then recreat the original view.) 
         IF SQLCODE IN (-4063) THEN
            NULL;
         --Raise exception along with the statement that failed.
         ELSE
            raise_application_error(-20000, 'Problem with this statement: ' ||
               objects.grantSQL || CHR(10) || SQLERRM);
         END IF;
      END;
   END LOOP;
END;
/
Jon Heller
  • 34,999
  • 6
  • 74
  • 132
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
1

If you want to grant privileges to all tables in a specific schema:

    BEGIN
    FOR x IN (select *from all_tables where OWNER = 'schema name')
    LOOP   
     EXECUTE IMMEDIATE 'GRANT SELECT ON '||x.OWNER||'.'|| x.table_name || TO 'user name'; 
    END LOOP;
    END;
-1
begin
  for x in (select *from all_tables where owner = 'SYS')
  loop   
    execute immediate 'grant select on '||x.owner||'.'|| x.table_name || ' to ' || 'your_user';
  end loop;
end;
  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 02 '21 at 13:05