Can GRANT be used inside an Oracle Store Procedure?

后端 未结 2 877
梦毁少年i
梦毁少年i 2020-12-21 05:24

When trying to place a GRANT statement in an Oracle 11 stored procedure, it reports that GRANT is an unexpected symbol. Does GRANT need to be prefaced by something, or does

2条回答
  •  悲&欢浪女
    2020-12-21 05:54

    Here's a PL/SQL stored procedure that grants object privileges (SELECT, UPDATE, etc.) on all tables owned by the current user to another user, e.g. - "appuser".

    CREATE OR REPLACE PROCEDURE grant_privs
    IS
       CURSOR ut_cur IS SELECT table_name from user_tables;
       ut_rec ut_cur%rowtype;
    BEGIN
       FOR ut_rec IN ut_cur
       LOOP
          EXECUTE IMMEDIATE 'GRANT ALL ON ' || ut_rec.table_name || ' TO appuser';
       END LOOP;
    END;
    /
    

    It was executed automatically after deploying database changes for a web application. The current user owns the database tables. After deploying any database changes, the stored procedure was executed to ensure that "appuser" could run SQL statements on all of the tables. The web application connected as "appuser", which had only limited system privileges, for security reasons.

    This is both a solution to the problem and a solid use case for the solution.

提交回复
热议问题