We cannot natively execute DDL in any form of PL/SQL. including triggers. To do that we need to use dynamic SQL.
Triggers have an additional wrinkle: they are fired as part of the transaction, and they have a limitation that forbids us from issuing a commit inside their body. In Oracle, any DDL command issues two commits, one before and one after the DDL statement is executed. So, to execute DDL in a trigger we must use the autonomous_transaction pragma
, which means the DDL runs in a separate, nested transaction.
create or replace TRIGGER TestTrigger
BEFORE INSERT ON TestTable
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
declare
pragma autonomous_transaction;
BEGIN
execute immediate 'create role '|| :New.RoleName;
END;
Autonomous transaction are one of those constructs which are easy for us to misuse and sabotage our own applications. In your scenario the snag is that the CREATE ROLE can succeed in its transaction bubble whilst the INSERTT into TestTable
fails; such is the meaning of "autonomous transaction". So you are still not guaranteed "coherence between [your] table and oracle roles one".
A better solution would be to wrap both statements into a procedural call, rather than attempting to trick DML into doing something it is not supposed to do.
create or replace procedure create_role
( p_role_name in user_roles.role%type
, p_desc in testtable.description%type )
is
pragma autonomous_transaction;
begin
insert into testtable
( id, creationdate, rolename, description)
values
( some_seq.nextval, sysdate, p_role_name, p_desc );
execute immediate 'create role '|| p_role_name;
end;
CREATE
, but I have no idea if Roles can be created like this. – Drumbegexecute immediate
; but not inside a trigger because you can't commit inside a trigger, and DDL does an implicit commit. You can get around that by making it autonomous, or by submitting a job, but this is getting a bit convoluted. Seems an odd requirement. If you have to create roles from your application, why not only create a role - what does duplicating it in your table add? – Alex Poole