Control a trigger using when clause in Oracle - oracle

I am trying to create trigger TRIG_USER in Oracle db. Is there a way to control this trigger using the when clause.I want the trigger to activate only if there a entry in the table trigger_switch
I am looking something like this.
CREATE TRIGGER TRIG_USER
AFTER INSERT OR UPDATE OR DELETE ON USER_TABLE
FOR EACH ROW WHEN (IF EXISTS (SELECT 1 FROM trigger_switch))
BEGIN
[...]
END;

Related

How to get the table name in trigger on which ddl operation will be performed

I want to do something like
create or replace trigger t1 before ddl on database
begin
insert into table1 values(//the table name on which the ddl will be performed);
end;
so if i create a table named "Hello" than "Hello"(table name) will be inserted in table1
so i don't know how to fetch the table name actually
That's show here in the Database Docs
So you could do something like...
CREATE OR REPLACE TRIGGER ddl_trig
AFTER DDL
ON DATABASE
BEGIN
INSERT INTO loguser.ddl_log
(user_name, ddl_date, ddl_type,
object_type, owner,
object_name)
VALUES
(ora_login_user, SYSDATE, ora_sysevent,
ora_dict_obj_type, ora_dict_obj_owner,
ora_dict_obj_name);
END ddl_trig;
/
BUT, you shouldn't build your own auditing software/code. You should instead use the database's built in auditing system.
See this.
Disclaimer: I work for Oracle and am a product manager on the database team.

DB Insertion Issue on Materialized View in PSQL

I have been working on creating the trigger for one materialized view like this in PSQL using supruser:
returns trigger language plpgsql
as $$
begin
refresh materialized view mat_view;
return null;
end $$;
create trigger refresh_mat_view
after insert or update or delete or truncate
on table1 for each statement
execute procedure refresh_mat_view();
create trigger refresh_mat_view
after insert or update or delete or truncate
on table2 for each statement
execute procedure refresh_mat_view();
After set up the trigger, when others tried to insert records in table1, it showed failed to insert and must be the owner of the mat_view. Is there any way that I can get around this for creating the trigger and also allow others to insert?
https://blog.rustprooflabs.com/2021/07/postgres-permission-mat-view
Issue can be solved here by creating a group of role to be act as an owner or using a cron job

Is there a way to log table creation and column modification in a table with the one who execute it, in oracle schema?

I am searching for a way, to store only the tables and columns that are added in the database with the one who created it (nachine_name) in a log table.
I tried to add a trigger on sys table user_tab_cols but I cannot do that Why cannot I create triggers on objects owned by SYS?
The system table user_objects will give me the date when a table created, but I want also to know which machine created it. and I also want to track the column creation and modification and log them in a table.
Is that possible ? is there a way for that ?
you can create a database event trigger:
CREATE OR REPLACE TRIGGER log_ddl_event_trg
AFTER DDL
ON DATABASE
DECLARE
v_sql_list ora_name_list_t;
v_sql_txt VARCHAR2(2500);
BEGIN
FOR i in 1..ORA_SQL_TXT(v_sql_list) LOOP
v_sql_txt := v_sql_txt || v_sql_list(i);
EXIT WHEN length(v_sql_txt ) >= 2000;
END LOOP;
...
END;
/
in the Trigger, you can get the executed ddl-statement using the ORA_SQL_TXT() Funktion, and then log it in the table together with the other data (log_date, user etc.).

How do I alter a trigger in Oracle?

I am trying to change the trigger script for my database. My trigger name is ARCH_USER_UPD_TRG and this puts any updates or deletes on the USER table into a Z_USER table
I am dropping a column from the USER table and now need to modify the trigger script to no longer use this column.
How do I modify the PL/SQL script of an oracle trigger?
A trigger is similar to a package or a procedure, so you can simply use
create or replace trigger triggerName
...
declare
...
begin
...
end;
The easy solution would be to Drop and Create the trigger once again with the modified SQL script code.
DROP TRIGGER ARCH_USER_UPD_TRG;
CREATE TRIGGER ARCH_USER_UPD_TRG
//rest of code body

Statement Triggers on a VIEW in Oracle

Is it possible in Oracle DB to create Statement trigger (but not Row trigger) on a VIEW?
When I create INSTEAD OF trigger without FOR EACH ROW option on a view, Oracle fires that trigger for each row any way.
For example, the following code:
CREATE TABLE TEST_TABLE (
MY_DATA VARCHAR(30)
);
INSERT INTO TEST_TABLE(MY_DATA) VALUES('one');
INSERT INTO TEST_TABLE(MY_DATA) VALUES('two');
INSERT INTO TEST_TABLE(MY_DATA) VALUES('three');
CREATE OR REPLACE VIEW TEST_VIEW AS
SELECT * FROM TEST_TABLE;
CREATE OR REPLACE TRIGGER TEST_VIEW_TRG1
INSTEAD OF DELETE ON TEST_VIEW
DECLARE
BEGIN
Dbms_Output.Put_Line('STATEMENT TRIGGER.');
END;
/
CREATE OR REPLACE TRIGGER TEST_VIEW_TRG2
INSTEAD OF DELETE ON TEST_VIEW FOR EACH ROW
DECLARE
BEGIN
Dbms_Output.Put_Line('ROW TRIGGER: '||:OLD.MY_DATA);
END;
/
DELETE FROM TEST_VIEW;
Produces the following output:
ROW TRIGGER: one
STATEMENT TRIGGER.
ROW TRIGGER: two
STATEMENT TRIGGER.
ROW TRIGGER: three
STATEMENT TRIGGER.
When I create triggers TEST_VIEW_TRG1 and TEST_VIEW_TRG2 as AFTER on a TEST_TABLE (instead of a TEST_VIEW) the output is as expected:
ROW TRIGGER: one
ROW TRIGGER: two
ROW TRIGGER: three
STATEMENT TRIGGER.
Is there any workaround for this issue?
INSTEAD OF Triggers on views are always row-based, as stated in the Docs: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_7004.htm#i2235611
FOR EACH ROW
Specify FOR EACH ROW to designate the trigger as a row trigger. Oracle Database fires a row trigger once for each row that is affected by the triggering statement and meets the optional trigger constraint defined in the WHEN condition.
Except for INSTEAD OF triggers, if you omit this clause, then the trigger is a statement trigger. Oracle Database fires a statement trigger only once when the triggering statement is issued if the optional trigger constraint is met.
INSTEAD OF trigger statements are implicitly activated for each row.

Resources