有时要在测试环境创建跟生产环境同个用户,但又不知道用户相关的权限,这里提供一个实用的脚本。
获取创建用户脚本及权限
脚本内容如下:
--执行存储过程 exec DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR', true); --查询用户脚本及权限 SELECT ( CASE WHEN ((SELECT COUNT(*) FROM dba_users WHERE username = '&&Username') > 0) THEN dbms_metadata.get_ddl ('USER', '&&Username') ELSE to_clob (' -- Note: User not found!') END ) extracted_ddl FROM dual UNION ALL SELECT ( CASE WHEN ((SELECT COUNT(*) FROM dba_ts_quotas WHERE username = '&&Username') > 0) THEN dbms_metadata.get_granted_ddl( 'TABLESPACE_QUOTA', '&&Username') ELSE to_clob (' -- Note: No TS Quotas found!') END ) FROM dual UNION ALL SELECT ( CASE WHEN ((SELECT COUNT(*) FROM dba_role_privs WHERE grantee = '&&Username') > 0) THEN dbms_metadata.get_granted_ddl ('ROLE_GRANT', '&&Username') ELSE to_clob (' -- Note: No granted Roles found!') END ) FROM dual UNION ALL SELECT ( CASE WHEN ((SELECT COUNT(*) FROM dba_sys_privs WHERE grantee = '&&Username') > 0) THEN dbms_metadata.get_granted_ddl ('SYSTEM_GRANT', '&&Username') ELSE to_clob (' -- Note: No System Privileges found!') END ) FROM dual UNION ALL SELECT ( CASE WHEN ((SELECT COUNT(*) FROM dba_tab_privs WHERE grantee = '&&Username') > 0) THEN dbms_metadata.get_granted_ddl ('OBJECT_GRANT', '&&Username') ELSE to_clob (' -- Note: No Object Privileges found!') END ) FROM dual
结果如下:
有些场景还是会用到的,大家有兴趣可以自己测试一下。
后面会分享更多DBA方面内容,感兴趣的朋友可以关注一下~
,