Skip to content
Advertisement

Is it possible to use GRANT inside a trigger in Oracle 18c?

I try to create a trigger which automatically grants select on all new tables for a specific schema, whenever a new table in this schema is created.

Background for this is IBM InfoSphere Information Server’s Exception Database. This tool creates new tables for exceptions that are created in DataStage Jobs and I want a group of developers to be able to query these tables without giving them permission to the owner of the schema.

So my idea was to create a trigger like this:

But I get error “PLS-00103” after compiling the trigger. It says, that “GRANT” is not expected and it expects one of the following instead:

Sounds to me that GRANT is not allowed inside a trigger. If that’s so, is there another way to automatically grant users select-permission to new tables inside a specific schema?

Advertisement

Answer

Error you got says that you can’t execute DDL (yes, grant is a DDL) like that – it has to be done as dynamic SQL, using execute immediate.

However, it won’t help in this case because DDL implicitly commits, and you can’t commit within a trigger.

Now you’ll say that you can create a trigger as an autonomous transaction. Well, yes – you can, but it wouldn’t help in this case because the table is yet to be created (i.e. it doesn’t exist yet).


Here’s a workaround; see if it helps. In a few words:

  • create an auxiliary procedure (to make it simpler) which will, actually, perform grant operation
  • let trigger submit a job which will call that procedure

Here’s how: I’m connected as Scott and will be granting privileges to user Mike (as I don’t have your users):

Testing:

Connect as Mike and check what it sees:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement