生成脚本[ORACLE]

时间:2016-12-07 12:50:19

标签: sql oracle plsql oracle12c

如何编写获取脚本的查询,以删除用户c ## test_user的某些表中删除记录的权限给拥有它的用户?

我尝试了这个查询:

SELECT GRANTEE, TABLE_NAME 
FROM DBA_TAB_PRIVS 
WHERE PRIVILEGE ='DELETE' 
AND TABLE_NAME IN (SELECT TABLE_NAME 
                   FROM ALL_TABLES 
                   WHERE OWNER = 'c##test_user');

问题是我不知道如何从select生成脚本。有任何想法吗?。谢谢

1 个答案:

答案 0 :(得分:3)

这样做

declare
cursor DeleteGrants is
SELECT GRANTEE, TABLE_NAME 
FROM DBA_TAB_PRIVS 
WHERE PRIVILEGE ='DELETE' 
AND TABLE_NAME IN (SELECT TABLE_NAME 
                   FROM ALL_TABLES 
                   WHERE OWNER = 'c##test_user');

begin
   for aGrant in DeleteGrants loop
      execute immediate 'REVOKE DELETE ON c##test_user.'||aGrant.TABLE_NAME||' FROM '||aGrant.GRANTEE;
   end loop;
end;

如果你只想输出但不执行,你也可以

SELECT 'REVOKE DELETE ON c##test_user.'||TABLE_NAME||' FROM '||GRANTEE||';' as cmd
FROM DBA_TAB_PRIVS 
WHERE PRIVILEGE ='DELETE' 
AND TABLE_NAME IN (SELECT TABLE_NAME 
                   FROM ALL_TABLES 
                   WHERE OWNER = 'c##test_user');