oracle 对特定用户拥有的所有表授予Select权限

b5lpy0ml  于 2023-01-20  发布在  Oracle
关注(0)|答案(5)|浏览(388)

我需要将一个特定用户拥有的所有表的选择权限授予另一个用户。我可以沿着以下命令行来完成此操作吗:

Grant Select on OwningUser.* to ReceivingUser

或者,我是否必须为每个表生成SQL,并使用以下代码:

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

krugob8w1#

这不是一个简单的语句,但它与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;
eqoofvh9

eqoofvh92#

表+视图+错误报告

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

7vhp5slm3#

是的,这是可能的,运行此命令:
假设您的用户名为thoko

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

注:使用oracle数据库

vmdwslir

vmdwslir4#

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;
8yparm6h

8yparm6h5#

DECLARE
    v_owner VARCHAR2(50) DEFAULT 'TEST1';
    v_grantee VARCHAR2(50) DEFAULT 'TEST2';
BEGIN
    FOR c IN (SELECT object_name FROM all_objects WHERE owner = v_owner and object_type = 'TABLE')
    LOOP
        BEGIN
            EXECUTE IMMEDIATE 'GRANT SELECT ON ' || v_owner || '.' || c.object_name||' TO ' || v_grantee;
        EXCEPTION when OTHERS THEN
            DBMS_OUTPUT.PUT_LINE (c.object_name);
        END;
    END LOOP;
END;
/

相关问题