如何在Oracle中获取SCHEMA的所有序列授权DDL

sqserrrh  于 2023-03-01  发布在  Oracle
关注(0)|答案(2)|浏览(151)

尝试以下查询以获取序列的权限,

select * from dba_tab_privs where owner='TEST' and TYPE='SEQUENCE';

回复:

GRANTEE      OWNER  TABLE_NAME  GRANTOR PRIVILEGE   GRANTABLE   HIERARCHY   COMMON  TYPE    INHERITED

READ_WRITE   TEST   TEST_SEQ    TEST    SELECT       NO         NO            NO    SEQUENCE    NO

但是我如何得到下面的DDL?

grant select on TEST.TEST_SEQ to READ_WRITE;

我已经尝试使用下面的查询获取序列的DDL,

SELECT dbms_metadata.get_ddl(object_type, object_name, owner) || ';' AS object_ddl
FROM DBA_OBJECTS
WHERE OWNER = 'TEST' AND OBJECT_TYPE IN ('SEQUENCE')
ORDER BY OWNER, OBJECT_TYPE, OBJECT_NAME;
qvk1mo1f

qvk1mo1f1#

正确的调用是:

select dbms_metadata.get_dependent_ddl('OBJECT_GRANT','TEST','TEST_SEQ') from dual

您也可以轻松地自己编写脚本:

SELECT 'GRANT '||privilege||' ON "'||owner||'"."'||table_name||'" TO "'||grantee||'";'
  FROM dba_tab_privs
 WHERE (owner,table_name) IN (SELECT sequence_owner,sequence_name
                                FROM dba_sequences
                               WHERE sequence_owner = 'TEST')
hc2pp10m

hc2pp10m2#

您不需要查看DDL,可以通过将列值连接在一起,从已有内容生成语句:

select 'GRANT ' || privilege || ' ON "' || owner || '"."' || table_name || '" TO "' || grantee || '";'
from dba_tab_privs where owner='TEST' and TYPE='SEQUENCE';

您可能不需要双引号,但我通常使用双引号,以防数据库中潜伏着任何带引号的标识符。
如果你真的想使用dbms_metadata,那么你需要get_dependent_ddl函数来代替get_ddl

select dbms_metadata.get_dependent_ddl('OBJECT_GRANT', table_name, owner)
from dba_tab_privs where owner='TEST' and type='SEQUENCE';

这将给予类似引用的标识符。

相关问题