在特定用户拥有的所有表上授予选择权限

时间:2008-10-09 15:44:30

标签: oracle

我需要为特定用户拥有的所有表授予对另一个用户的select权限。我可以使用以下命令执行此操作:

Grant Select on OwningUser.* to ReceivingUser

或者我是否必须为每个表生成sql,其中包含以下内容:

 Select 'GRANT SELECT ON OwningUser.'||Table_Name||'TO ReceivingUser' 
 From All_Tables Where Owner='OWNINGUSER'

4 个答案:

答案 0 :(得分:53)

嗯,这不是一个单一的声明,但是它与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; 

答案 1 :(得分:2)

表格+观看次数+错误报告

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;
/

答案 2 :(得分:1)

是的,可能的,运行此命令:

假设您有名为thoko的用户

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

注意:在oracle数据库上工作

答案 3 :(得分:0)

http://psoug.org/reference/roles.html,在您的数据库上创建一个程序供您的用户执行:

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;