Grant Select on all Tables Owned By Specific User

后端 未结 4 1155
春和景丽
春和景丽 2020-12-02 16:55

I need to grant select permission for all tables owned by a specific user to another user. Can I do this with a single command along the lines of:

Grant Sel         


        
相关标签:
4条回答
  • 2020-12-02 17:20

    tables + views + error reporting

    SET SERVEROUT ON
    DECLARE
      o_type VARCHAR2(60) := '';
      o_name VARCHAR2(60) := '';
      o_owner VARCHAR2(60) := '';
      l_error_message VARCHAR2(500) := '';
    BEGIN
      FOR R IN (SELECT owner, object_type, object_name
                FROM all_objects 
                WHERE owner='SCHEMANAME'
                AND object_type IN ('TABLE','VIEW')
                ORDER BY 1,2,3) LOOP
        BEGIN
        o_type := r.object_type;
        o_owner := r.owner;
        o_name := r.object_name;
        DBMS_OUTPUT.PUT_LINE(o_type||' '||o_owner||'.'||o_name);
        EXECUTE IMMEDIATE 'grant select on '||o_owner||'.'||o_name||' to USERNAME';
        EXCEPTION
          WHEN OTHERS THEN
            l_error_message := sqlerrm;
            DBMS_OUTPUT.PUT_LINE('Error with '||o_type||' '||o_owner||'.'||o_name||': '|| l_error_message);
            CONTINUE;
        END;
      END LOOP;
    END;
    /
    
    0 讨论(0)
  • 2020-12-02 17:27

    yes, its possible, run this command:

    lets say you have user called thoko

    grant select any table, insert any table, delete any table, update any table to thoko;
    

    note: worked on oracle database

    0 讨论(0)
  • 2020-12-02 17:35

    From http://psoug.org/reference/roles.html, create a procedure on your database for your user to do it:

    CREATE OR REPLACE PROCEDURE GRANT_SELECT(to_user in varchar2) AS
    
      CURSOR ut_cur IS SELECT table_name FROM user_tables;
    
      RetVal  NUMBER;
      sCursor INT;
      sqlstr  VARCHAR2(250);
    
    BEGIN
        FOR ut_rec IN ut_cur
        LOOP
          sqlstr := 'GRANT SELECT ON '|| ut_rec.table_name || ' TO ' || to_user;
          sCursor := dbms_sql.open_cursor;
          dbms_sql.parse(sCursor,sqlstr, dbms_sql.native);
          RetVal := dbms_sql.execute(sCursor);
          dbms_sql.close_cursor(sCursor);
    
        END LOOP;
    END grant_select;
    
    0 讨论(0)
  • 2020-12-02 17:45

    Well, it's not a single statement, but it's about as close as you can get with oracle:

    BEGIN
       FOR R IN (SELECT owner, table_name FROM all_tables WHERE owner='TheOwner') LOOP
          EXECUTE IMMEDIATE 'grant select on '||R.owner||'.'||R.table_name||' to TheUser';
       END LOOP;
    END; 
    
    0 讨论(0)
提交回复
热议问题