授予对table1的选择(用户列表)

时间:2018-07-27 13:12:36

标签: sql oracle plsql

我的目标是将选择权授予以下三个表格中的所有受赠人。 我无法运行下面的plsql脚本。

以下陈述起作用: 允许选择ZIP压缩到用户_0;

我将其放入for每个循环中,并收到以下错误: 不允许使用“表格,视图或序列参考” 此上下文”

在这种情况下如何引用该表?

CREATE OR REPLACE PROCEDURE granting AS
BEGIN
    FOR users IN (SELECT DISTINCT GRANTEE FROM USER_TAB_PRIVS where TABLE_NAME = 'LAND' or TABLE_NAME = 'STREET' or TABLE_NAME = 'ZIP')
      LOOP
        BEGIN
          EXECUTE IMMEDIATE 'GRANT SELECT ON ''' || PLZORT || ''' TO ''' ||  users||'';
          EXECUTE IMMEDIATE 'GRANT SELECT ON ''' || STREET || ''' TO ''' ||  users||'';
          EXECUTE IMMEDIATE 'GRANT SELECT ON ''' || ZIP || ''' TO ''' ||  users||'';
        END;
      END LOOP;
END granting ;

2 个答案:

答案 0 :(得分:1)

请在下面的示例中查找

CREATE OR REPLACE PROCEDURE granting AS
BEGIN
    FOR rec IN (SELECT DISTINCT GRANTEE, TABLE_NAME FROM USER_TAB_PRIVS where TABLE_NAME in ('LAND' , 'STREET' , 'ZIP'))
      LOOP
        BEGIN
          EXECUTE IMMEDIATE 'GRANT SELECT ON ' || rec.TABLE_NAME || ' TO ' || rec.GRANTEE;
        END;
      END LOOP;
END granting ;

答案 1 :(得分:1)

如果只需要授予对这3个表的访问权限,则可以执行以下操作:

CREATE OR REPLACE PROCEDURE granting AS
BEGIN
    FOR users IN (SELECT DISTINCT GRANTEE FROM USER_TAB_PRIVS where TABLE_NAME = 'LAND' or TABLE_NAME = 'STREET' or TABLE_NAME = 'ZIP')
      LOOP
        BEGIN
          EXECUTE IMMEDIATE 'GRANT SELECT ON PLZORT TO ' ||  users.GRANTEE ;
          EXECUTE IMMEDIATE 'GRANT SELECT ON STREET TO ' ||  users.GRANTEE ;
          EXECUTE IMMEDIATE 'GRANT SELECT ON ZIP  TO ' ||  users.GRANTEE ;
        END;
      END LOOP;
END granting ;

但是您更可能希望管理用户组。为此,您可能更愿意创建角色。像这样:

CREATE ROLE ROLE_ADDRES_RO;

然后,您可以授予该角色对此表的访问权限:

GRANT SELECT ON PLZORT TO ROLE_ADDRES_RO; 
GRANT SELECT ON STREET TO ROLE_ADDRES_RO; 
GRANT SELECT ON ZIP    TO ROLE_ADDRES_RO; 
GRANT SELECT ON LAND   TO ROLE_ADDRES_RO; 

然后手动或通过类似的脚本将此角色授予用户列表:

DECLARE
BEGIN
    FOR users IN (SELECT DISTINCT GRANTEE FROM USER_TAB_PRIVS where TABLE_NAME = 'LAND' or TABLE_NAME = 'STREET' or TABLE_NAME = 'ZIP')
      LOOP
        BEGIN
          EXECUTE IMMEDIATE 'GRANT ROLE_ADDRES_RO TO ' ||  users.GRANTEE ;
        END;
      END LOOP;
END ;

在这种情况下,角色的下一次更新将更加透明。并且只能通过一个数据库调用来授予