如何在 Oracle 中获取 SCHEMA 的所有 SEQUENCES GRANT DDL

How to get all the SEQUENCES GRANT DDL for the SCHEMA in Oracle

提问人:Satscreate 提问时间:2/24/2023 更新时间:2/25/2023 访问量:314

问:

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

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;
数据库 Oracle Oracle-SQLDader 序列

评论


答:

0赞 Alex Poole 2/25/2023 #1

您无需查看 DDL,可以通过将列值连接在一起来从已有的语句中生成语句:

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

您可能不需要双引号,但我通常会包含它们,以防万一您的数据库中潜伏着任何带引号的标识符。

如果你真的想使用,那么你需要这个函数而不是:dbms_metadataget_dependent_ddlget_ddl

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

这将提供类似引号的标识符。

0赞 Paul W 2/25/2023 #2

正确的调用是:

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')