Grant for several Oracle tables

Asked

Viewed 3,456 times

1

I need to create a database user, where he can have permission from all DML, but when creating Grant I have to specify table by table, there is some way to give permission to the whole database, follow my example:

GRANT select, update, delete, insert ON BANCO01.TABELA01 TO STACKOVERFLOW

However, as I said, I’m having to set table by table!

2 answers

1

I like to generate the scripts of grant at hand, because we usually perform once, through a query:

SELECT 'GRANT select,update,delete,insert ON ' || x.table_name || ' TO STACKOVERFLOW;'
  FROM all_tables x
 WHERE x.owner = USER;

Then the result I copy and execute.


Or the same case using BULK COLLECT, when we have a large volume of tables:

DECLARE
  TYPE t_cursor IS REF CURSOR;
  TYPE t_string_array IS TABLE OF VARCHAR2(1000) INDEX BY BINARY_INTEGER;
  vcursor      t_cursor;
  varraystring t_string_array;
  i            BINARY_INTEGER;

BEGIN
  OPEN vcursor FOR
    SELECT 'GRANT select,update,delete,insert ' || x.table_name || ' TO STACKOVERFLOW'
      FROM all_tables x
     WHERE x.owner = USER;

  LOOP
    FETCH vcursor BULK COLLECT
      INTO varraystring;
    EXIT WHEN varraystring.count = 0;
    FOR i IN varraystring.first .. varraystring.last LOOP
      EXECUTE IMMEDIATE varraystring(i);
    END LOOP;
    EXIT WHEN vcursor%NOTFOUND;
  END LOOP;
  CLOSE vcursor;

END;

There are other more complex ways to do it, but I believe this one solves your case.

0

You can also use EXECUTE in a loop. Ex:

FOR x IN (SELECT * FROM all_tables)
LOOP   
EXECUTE IMMEDIATE 'GRANT select,update,delete,insert ON ' || x.table_name || ' TO STACKOVERFLOW'; 
END LOOP;
  • Excuse the question, but when I run the code above, changing to my real parameters, giving syntax error, I need to pay attention to some conditional ?

  • Maybe you need the BEGIN and END; respectively at the beginning and at the end if the script is not recognized. If it works I update the response :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.