【问题标题】:Want to grant select to a specific user on all tables想要将选择授​​予所有表上的特定用户
【发布时间】:2022-08-14 09:16:50
【问题描述】:

我使用以下命令授予 amy 在 table1 上的选择权限:

grant select schema.table1 to amy;

但是,以下不起作用:

grant select on schema.* to amy;

错误是

ORA-00903: 无效的表名

请告知我的命令有什么问题。我想要的是在所有表上授予 amy 选择权限。

谢谢

标签: sql oracle


【解决方案1】:

The syntax for the GRANT command(从 Oracle 21c 开始)明确表示授予的项目只能是特定对象:

on_object_clause:

您可以做的最好的事情是创建一个角色并将该角色授予 Amy,但您仍然必须将每个单独表的权限授予该角色。

create role analyst;
grant analyst to amy;

grant read on hr.countries to analyst;
grant read on hr.departments to analyst;
grant read on hr.employees to analyst;

【讨论】:

  • 谢谢回复,如果我有 100 个表需要授予 Amy,我已经反复对 table1 进行授予阅读 100 次?如果是的话,有没有什么办法可以通过一个脚本而不是100次来完成?
【解决方案2】:

要创建角色并将其授予 amy:

create role select_all_tables;
grant select_all_tables to amy;
alter user amy default role select_all_tables;

使用以下查询生成脚本,您可以复制/粘贴并运行该脚本以完成所有授权:

select 'grant select on '|| owner || '.' || table_name ||' to select_all_tables;' 
from dba_tables where owner='SCHEMA';

复制并粘贴输出,并将其作为脚本执行。

grant select on schema.table1 to select_all_tables;
grant select on schema.table2 to select_all_tables;
...

如果您希望直接授予权限而不使用角色,请更改脚本查询:

select 'grant select on '|| owner || '.' || table_name ||' to amy;' 
from dba_tables where owner='SCHEMA';

然后将输出作为脚本运行:

grant select on schema.table1 to amy;
grant select on schema.table2 to amy;
...

【讨论】:

    【解决方案3】:
    grant select on table to public;
    
    

    但是当你这样做时要小心——确保这是你真正想做的。

    【讨论】:

    • PUBLIC 不完全是“特定用户”(这是 OP 想要的)。而且它仍然授予单个表的特权,所以......你的意思是什么?
    猜你喜欢
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多