在SYS用户下执行:
CREATE OR REPLACE PROCEDURE P_CRSYN_GRANT(V_USER IN VARCHAR2, V_OBJECT_OWNER IN VARCHAR2) IS ----V_USER 需要授权的用户 ----V_OBJECT_OWNER 对象所有者 CURSOR C_SQL_STR IS SELECT ‘CREATE OR REPLACE PUBLIC SYNONYM ‘||OBJECT_NAME ||‘ FOR ‘||V_OBJECT_OWNER||‘.‘||OBJECT_NAME CREATE_SQL FROM DBA_OBJECTS WHERE OBJECT_TYPE IN (‘TABLE‘, ‘VIEW‘, ‘PROCEDURE‘, ‘TRIGGER‘, ‘FUNCTION‘, ‘PACKAGE‘,‘SEQUENCE‘) AND OWNER = UPPER(V_OBJECT_OWNER); --定义索引表数据类型 TYPE REG_TABLE_TYPE IS TABLE OF VARCHAR2(1000) INDEX BY BINARY_INTEGER; --声明索引表数据类型的变量 V_SELECT_TABLE REG_TABLE_TYPE; --定义参考游标数据类型 TYPE V_REF_CURSOR IS REF CURSOR ; --声明参考游标数据类型的变量 C_SQL V_REF_CURSOR; V_GRANT_STR VARCHAR2(2000); BEGIN FOR V IN C_SQL_STR LOOP EXECUTE IMMEDIATE V.CREATE_SQL; END LOOP; V_SELECT_TABLE(1) := ‘SELECT ‘‘GRANT SELECT,INSERT,UPDATE,DELETE ON ‘‘ ||OBJECT_NAME || ‘‘ TO ‘‘||‘‘‘||V_USER ||‘‘‘ SQLSTR FROM DBA_OBJECTS WHERE OBJECT_TYPE =‘‘TABLE‘‘ AND OWNER = UPPER(‘‘‘||V_OBJECT_OWNER||‘‘‘)‘; V_SELECT_TABLE(2) := ‘SELECT ‘‘GRANT SELECT,INSERT,UPDATE,DELETE ON ‘‘ ||OBJECT_NAME || ‘‘ TO ‘‘||‘‘‘||V_USER ||‘‘‘ SQLSTR FROM DBA_OBJECTS WHERE OBJECT_TYPE =‘‘VIEW‘‘ AND OWNER = UPPER(‘‘‘||V_OBJECT_OWNER||‘‘‘)‘; V_SELECT_TABLE(3) := ‘SELECT ‘‘GRANT EXECUTE ON ‘‘ ||OBJECT_NAME || ‘‘ TO ‘‘||‘‘‘||V_USER ||‘‘‘ SQLSTR FROM DBA_OBJECTS WHERE OBJECT_TYPE =‘‘PROCEDURE‘‘ AND OWNER = UPPER(‘‘‘||V_OBJECT_OWNER||‘‘‘)‘; V_SELECT_TABLE(4) := ‘SELECT ‘‘GRANT EXECUTE ON ‘‘ ||OBJECT_NAME || ‘‘ TO ‘‘||‘‘‘||V_USER ||‘‘‘ SQLSTR FROM DBA_OBJECTS WHERE OBJECT_TYPE =‘‘FUNCTION‘‘ AND OWNER = UPPER(‘‘‘||V_OBJECT_OWNER||‘‘‘)‘; V_SELECT_TABLE(5) := ‘SELECT ‘‘GRANT SELECT ON ‘‘ ||OBJECT_NAME || ‘‘ TO ‘‘||‘‘‘||V_USER ||‘‘‘ SQLSTR FROM DBA_OBJECTS WHERE OBJECT_TYPE =‘‘SEQUENCE‘‘ AND OWNER = UPPER(‘‘‘||V_OBJECT_OWNER||‘‘‘)‘; FOR INT IN V_SELECT_TABLE.FIRST .. V_SELECT_TABLE.LAST LOOP OPEN C_SQL FOR V_SELECT_TABLE(INT); LOOP FETCH C_SQL INTO V_GRANT_STR; EXIT WHEN C_SQL%NOTFOUND; EXECUTE IMMEDIATE V_GRANT_STR; END LOOP; CLOSE C_SQL; END LOOP; END;
时间: 2024-11-08 00:35:32