• Arun Kumar

Grant Select on all tables in a schema

I encountered this situation where I wanted to grant SELECT on all the tables owned by one user to another user.


There are two simple ways to achieve this:



Generate SQL for each table


In this method, we will generate GRANT SELECT statement for each table using below query. Here we are trying to GRANT SELECT on each table owned by HR user to SCOTT user:


Select 'GRANT SELECT ON HR.'||Table_Name||' TO SCOTT;' 
From All_Tables Where Owner='HR';

Here is the sample output of the above command:

oracle-sql-grant
oracle-sql-grant

Now you can execute individual statements from the above output to GRANT SELECT ON all HR tables to SCOTT user.



Using simple LOOP statement


We can take use of PL/SQL and write a simple loop statement as below to automatically execute the GRANT SELECT statements for us. Again we are trying to GRANT SELECT on every table owned by HR user to SCOTT user:


BEGIN
   FOR R IN (SELECT owner, table_name FROM all_tables WHERE owner='HR') LOOP
      EXECUTE IMMEDIATE 'grant select on '||R.owner||'.'||R.table_name||' to SCOTT';
   END LOOP;
END;
/

Here is the sample output of the above command:

grant-select-on-all-schema-tables
grant-select-on-all-schema-tables

2,461 views

Recent Posts

See All

Generate Table DDL Command

The DBMS_METADATA package allows you to generate DDL commands for any of the database objects inside Oracle database. Generate Table DDL Command Generate View DDL Command Generate Materialized View DD

Temp Tablespace Utilization

It is very common that as a DBA you will encounter ‘ORA-1652: unable to extend temp segment’. Handling temporary tablespace is different than permanent tablespaces inside Oracle. On a high level, temp