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;
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;
/
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;
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;
/
5条答案
按热度按时间krugob8w1#
这不是一个简单的语句,但它与Oracle非常接近:
eqoofvh92#
表+视图+错误报告
7vhp5slm3#
是的,这是可能的,运行此命令:
假设您的用户名为
thoko
注:使用oracle数据库
vmdwslir4#
从http://psoug.org/reference/roles.html,在数据库上创建一个过程,以便用户执行此操作:
8yparm6h5#