24

I have 3 tables table1,table2,table3. I want to grant(select for example) these tables to a user, user1.

I know that I can grant with:

grant select on table1 to user1;
grant select on table2 to user1;
grant select on table3 to user1;

Can I grant the 3 tables to user1 using only 1 query?

Thanks

Sergio Martinez
  • 925
  • 1
  • 7
  • 20
  • 1
    possible duplicate of [Oracle SQL privelege authorization on multiple attributes and tables with one statement](http://stackoverflow.com/questions/13106087/oracle-sql-privelege-authorization-on-multiple-attributes-and-tables-with-one-st) – Jon Heller Mar 16 '13 at 18:57

5 Answers5

25

No. As the documentation shows, you can only grant access to one object at a time.


Oracle Database 23c has extended grant to allow you to give one user access to all tables in another schema:

grant select any table
  on schema table_owner
  to query_user;
Chris Saxon
  • 9,105
  • 1
  • 26
  • 42
9

You can do it with dynamic query, just run the following script in pl-sql or sqlplus:

select 'grant select on user_name_owner.'||table_name|| 'to user_name1 ;' from dba_tables t where t.owner='user_name_owner'

and then execute result.

Krypton
  • 3,337
  • 5
  • 32
  • 52
m_abr
  • 91
  • 1
  • 1
5

my suggestion is...create role in oracle using

create role <role_name>;

then assign privileges to that role using

grant select on <table_name> to <role_name>;

then assign that group of privileges via that role to any user by using

grant  <role_name> to <user_name>...;
Thiyagu ATR
  • 2,224
  • 7
  • 30
  • 44
0

This worked for me on my Oracle database:

SELECT   'GRANT SELECT, insert, update, delete ON mySchema.' || TABLE_NAME || ' to myUser;'
FROM     user_tables
where table_name like 'myTblPrefix%'

Then, copy the results, paste them into your editor, then run them like a script.

You could also write a script and use "Execute Immediate" to run the generated SQL if you don't want the extra copy/paste steps.

mang
  • 163
  • 1
  • 1
  • 14
0

If you want to grant to both tables and views try:

SELECT DISTINCT
    || OWNER
    || '.'
    || TABLE_NAME
    || ' to db_user;'
FROM
    ALL_TAB_COLS 
WHERE
    TABLE_NAME LIKE 'TABLE_NAME_%';

For just views try:

SELECT
    'grant select on '
    || OWNER
    || '.'
    || VIEW_NAME
    || ' to REPORT_DW;'
FROM
    ALL_VIEWS
WHERE
    VIEW_NAME LIKE 'VIEW_NAME_%';

Copy results and execute.

Aba
  • 584
  • 6
  • 11