Predicate | Description |
---|---|
Inserting | True if trigger triggered on Insert operation |
Updating | True if trigger triggered on Update operation |
Updating ('colum') | True if the trigger was triggered by an Update operation that affects a specific field. |
Deleting | True if trigger triggered on Delete operation |
CREATE OR REPLACE TRIGGER t BEFORE INSERT OR UPDATE OF salary, department_id OR DELETE ON employees BEGIN CASE WHEN INSERTING THEN DBMS_OUTPUT.PUT_LINE('Inserting'); WHEN UPDATING('salary') THEN DBMS_OUTPUT.PUT_LINE('Updating salary'); WHEN UPDATING('department_id') THEN DBMS_OUTPUT.PUT_LINE('Updating department ID'); WHEN DELETING THEN DBMS_OUTPUT.PUT_LINE('Deleting'); END CASE; END;
Trigger Operation | OLD.column | NEW.column |
---|---|---|
Insert | Null | New value |
Update | Old meaning | New value |
Delete | Old meaning | Null |
CREATE OR REPLACE VIEW order_info AS SELECT c.customer_id, c.cust_last_name, c.cust_first_name, o.order_id, o.order_date, o.order_status FROM customers c, orders o WHERE c.customer_id = o.customer_id; CREATE OR REPLACE TRIGGER order_info_insert INSTEAD OF INSERT ON order_info DECLARE duplicate_info EXCEPTION; PRAGMA EXCEPTION_INIT (duplicate_info, -00001); BEGIN INSERT INTO customers (customer_id, cust_last_name, cust_first_name) VALUES ( :new.customer_id, :new.cust_last_name, :new.cust_first_name); INSERT INTO orders (order_id, order_date, customer_id) VALUES ( :new.order_id, :new.order_date, :new.customer_id); EXCEPTION WHEN duplicate_info THEN RAISE_APPLICATION_ERROR ( num=> -20107, msg=> 'Duplicate customer or order ID'); END order_info_insert;
-- Create type of nested table element: CREATE OR REPLACE TYPE nte AUTHID DEFINER IS OBJECT ( emp_id NUMBER(6), lastname VARCHAR2(25), job VARCHAR2(10), sal NUMBER(8,2) ); / -- Created type of nested table: CREATE OR REPLACE TYPE emp_list_ IS TABLE OF nte; / -- Create view: CREATE OR REPLACE VIEW dept_view AS SELECT d.department_id, d.department_name, CAST (MULTISET (SELECT e.employee_id, e.last_name, e.job_id, e.salary FROM employees e WHERE e.department_id = d.department_id ) AS emp_list_ ) emplist FROM departments d; -- Create trigger: CREATE OR REPLACE TRIGGER dept_emplist_tr INSTEAD OF INSERT ON NESTED TABLE emplist OF dept_view REFERENCING NEW AS Employee PARENT AS Department FOR EACH ROW BEGIN -- Insert on nested table translates to insert on base table: INSERT INTO employees ( employee_id, last_name, email, hire_date, job_id, salary, department_id ) VALUES ( :Employee.emp_id, -- employee_id :Employee.lastname, -- last_name :Employee.lastname || '@company.com', -- email SYSDATE, -- hire_date :Employee.job, -- job_id :Employee.sal, -- salary :Department.department_id -- department_id ); END;
INSERT INTO TABLE ( SELECT d.emplist FROM dept_view d WHERE department_id = 10 ) VALUES (1001, 'Glenn', 'AC_MGR', 10000);
create or replace trigger tr_table_test_compound for update or delete or insert on table_test compound trigger v_count pls_integer := 0; before statement is begin dbms_output.put_line ( 'before statement' ); end before statement; before each row is begin dbms_output.put_line ( 'before insert' ); end before each row; after each row is begin dbms_output.put_line ( 'after insert' ); v_count := v_count + 1; end after each row; after statement is begin dbms_output.put_line ( 'after statement' ); end after statement; end tr_table_test_compound;
CREATE OR REPLACE TRIGGER drop_trigger BEFORE DROP ON hr.SCHEMA BEGIN RAISE_APPLICATION_ERROR ( num => -20000, msg => 'Cannot drop object'); END;
CREATE OR REPLACE TRIGGER check_user AFTER LOGON ON DATABASE BEGIN check_user; EXCEPTION WHEN OTHERS THEN RAISE_APPLICATION_ERROR (-20000, 'Unexpected error: '|| DBMS_Utility.Format_Error_Stack); END;
CREATE OR REPLACE TRIGGER t INSTEAD OF CREATE ON SCHEMA BEGIN EXECUTE IMMEDIATE 'CREATE TABLE T (n NUMBER, m NUMBER)'; END;
Attribute | Return value and type |
---|---|
ora_client_ip_address | Vararch2 client ip address Example: |
ora_database_name | Varchar2 (50) database name Example: |
ora_des_encrypted_password | Vararch2 DES-encrypted user password that is created or modified. Example: |
ora_dict_obj_name | Varchar2 (30) the name of the object on which the DDL operation is performed Example: |
ora_dict_obj_name_list ( name_list OUT ora_name_list_t ) | Pls_integer number of objects changed by the command Name_list - list of objects changed by the command Example: |
ora_dict_obj_owner | Varchar2 (30) the owner of the object on which the DDL operation is performed Example: |
ora_dict_obj_owner_list ( owner_list OUT ora_name_list_t ) | Pls_integer number of owners of objects changed by the team Owner_list - the list of owners of the objects changed by the command Example: |
ora_dict_obj_type | Varchar2 (20) the type of the object on which the ddl operation is performed Example: |
ora_grantee ( user_list OUT ora_name_list_t ) | Pls_integer number of users involved in the grant operation User_list is a list of these users. Example: |
ora_instance_num | Number instance number Example: |
ora_is_alter_column ( column_name IN VARCHAR2 ) | Boolean True if the specified field was changed by the alter operation. Otherwise false Example: |
ora_is_creating_nested_table | Boolean true if the current event is the creation of a nested table. Otherwise false Example: |
ora_is_drop_column ( column_name IN VARCHAR2 ) | Boolean true if the specified field is deleted. Otherwise false Example: |
ora_is_servererror ( error_number IN VARCHAR2 ) | Boolean true if an exception has been thrown with error_number number. Otherwise false Example: |
ora_login_user | Varchar2 (30) current user name Example: |
ora_partition_pos | Pls_integer in instead of trigger for create table position in the text of the sql command, where the partition construction can be inserted Example: |
ora_privilege_list ( privilege_list OUT ora_name_list_t ) | Pls_integer the number of privileges involved in a grant or revoke operation Privilege_list - a list of these privileges Example: |
ora_revokee ( user_list OUT ora_name_list_t ) | Pls_integer the number of users involved in the revoke operation User_list is a list of these users. Example: |
ora_server_error ( position IN PLS_INTEGER ) | Number error code in the specified position error stack, where 1 is the top of the stack Example: |
ora_server_error_depth | Pls_integer number of error messages in error stack Example: |
ora_server_error_msg ( position IN PLS_INTEGER ) | Vararch2 error message in the specified place error stack Example: |
ora_server_error_num_params ( position IN PLS_INTEGER ) | Pls_integer the number of rows replaced (using the% s format) in the specified position error stack Example: |
ora_server_error_param ( position IN PLS_INTEGER, param IN PLS_INTEGER ) | Vararch2 the replaced text in the error message in the specified position error stack (the param on the account returns the replaced text) Example: |
ora_sql_txt ( sql_text OUT ora_name_list_t ) | Pls_integer the number of items in the pl / sql collection in the sql_text. The parameter sql_text itself returns the command text for which the trigger worked. Example: |
ora_sysevent | Varchar2 (20) the name of the command that triggers Example: |
ora_with_grant_option | Boolean true if privileges are granted with a grant option. Otherwise false. Example: |
ora_space_error_info ( error_number OUT NUMBER error_type OUT VARCHAR2, object_owner out VARCHAR2, table_space_name OUT VARCHAR2, object_name OUT VARCHAR2, sub_object_name OUT VARCHAR2 ) | Boolean true if the error occurs due to lack of space. In the output parameters of the object. Example: |
Event | Description | Available Attributes |
---|---|---|
AFTER STARTUP | When starting the database. It happens only the database level. When an error is written to the system log. | ora_sysevent ora_login_user ora_instance_num ora_database_name |
BEFORE SHUTDOWN | Before the server starts the shutdown process. It happens only the database level. When an error is written to the system log. | ora_sysevent ora_login_user ora_instance_num ora_database_name |
AFTER DB_ROLE_CHANGE | When you start the database for the first time after changing roles from standby to primary or from primary to primary to standby. it is used only in the Data Guard configuration; there is only a database level. | ora_sysevent ora_login_user ora_instance_num ora_database_name |
AFTER SERVEREROR | If any error occurs (if with a condition, then it only works for the error specified in the condition). When an error occurs in the body of a trigger, it does not call itself recursively. | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_server_error ora_is_servererror ora_space_error_info |
BEFORE ALTER AFTER ALTER | If the object is changed by the alter command | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_dict_obj_type ora_dict_obj_name ora_dict_obj_owner ora_des_encrypted_password (for ALTER USER events) ora_is_alter_column (for ALTER TABLE events) ora_is_drop_column (for ALTER TABLE events) |
BEFORE DROP AFTER DROP | When deleting an object | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_dict_obj_type ora_dict_obj_name ora_dict_obj_owner |
BEFORE ANALYZE AFTER ANALYZE | When the analyze command is triggered | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_dict_obj_name ora_dict_obj_type ora_dict_obj_owner |
BEFORE ASSOCIATE STATISTICS AFTER ASSOCIATE STATISTICS | When executing associate statistics | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_dict_obj_name ora_dict_obj_type ora_dict_obj_owner ora_dict_obj_name_list ora_dict_obj_owner_list |
BEFORE AUDIT AFTER AUDIT BEFORE NOAUDIT AFTER NOAUDIT | When executing an audit or noaudit command | ora_sysevent ora_login_user ora_instance_num ora_database_name |
BEFORE COMMENT AFTER COMMENT | When adding a comment to an object | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_dict_obj_name ora_dict_obj_type ora_dict_obj_owner |
BEFORE CREATE AFTER CREATE | When creating an object | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_dict_obj_type ora_dict_obj_name ora_dict_obj_owner ora_is_creating_nested_table (for CREATE TABLE events) |
BEFORE DDL AFTER DDL | It works on most DDL commands, except: alter database, create control file, create database. | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_dict_obj_name ora_dict_obj_type ora_dict_obj_owner |
BEFORE DISASSOCIATE STATISTICS AFTER DISASSOCIATE STATISTICS | When you run the disassociate statistics command | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_dict_obj_name ora_dict_obj_type ora_dict_obj_owner ora_dict_obj_name_list ora_dict_obj_owner_list |
BEFORE GRANT AFTER GRANT | When executing the grant command | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_dict_obj_name ora_dict_obj_type ora_dict_obj_owner ora_grantee ora_with_grant_option ora_privilege_list |
BEFORE LOGOFF | It triggers before disconnecting a user, it can be a schema or database level | ora_sysevent ora_login_user ora_instance_num ora_database_name |
AFTER LOGON | It works after the user has successfully established a connection to the database. When an error prohibits the user from entering Does not affect SYS. | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_client_ip_address |
BEFORE RENAME AFTER RENAME | When executing the rename command | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_dict_obj_name ora_dict_obj_owner ora_dict_obj_type |
BEFORE REVOKE AFTER REVOKE | With the revoke command | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_dict_obj_name ora_dict_obj_type ora_dict_obj_owner ora_revokee ora_privilege_list |
AFTER SUSPEND | It works if the sql command is suspended due to a server error (out of memory). In this case, the trigger must change the conditions so that the command execution is resumed) | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_server_error ora_is_servererror ora_space_error_info |
BEFORE TRUNCATE AFTER TRUNCATE | When executing the truncate command | ora_sysevent ora_login_user ora_instance_num ora_database_name ora_dict_obj_name ora_dict_obj_type ora_dict_obj_owner |
ALTER TRIGGER [schema.]trigger_name { ENABLE | DISABLE };
ALTER TABLE table_name { ENABLE | DISABLE } ALL TRIGGERS;
alter trigger TRIGGER_NAME compile;
grant create trigger to USER;
grant create any trigger to USER; grant alter any trigger to USER; grant drop any trigger to USER;
grant ADMINISTER DATABASE TRIGGER to USER;
Source: https://habr.com/ru/post/256655/
All Articles