examples on trigger
TRANSCRIPT
-
8/7/2019 examples on trigger
1/18
Example: create a trigger on employee table called EMP to add rows on the table calledAUDIT_EMP_TABLE to trace any update operation on the table EMP. The triggershould store the old and new values of the different columns.
-
8/7/2019 examples on trigger
2/18
Use the USER_TRIGGERS data dictionary view to display information about the RESTRICT_SAL trigger.
SQL> SELECT trigger_name,trigger_type,
2 triggering_event, table_name,
referencing_names, when_clause, status,trigger_body
4 FROM user_triggers
5 WHEREtrigger_name = 'RESTRICT_SAL';
-
8/7/2019 examples on trigger
3/18
You can create a BEFORE statement trigger in order to prevent the triggering operation from succeeding if a certain condition isviolated.Create a trigger to restrict inserts into the EMP table to certain business hours, Monday through Friday.If a user attempts to insert a row into the EMP table on Saturday, for example, the user sees the message, the trigger fails, and thetriggering statement is rolled back. RAISE_APPLICATION_ERROR is a server-side built-in procedure that returns an error to theuser and causes the PL/SQL block to fail.
SQL> CREATE OR REPLACE TRIGGER secure_emp2 BEFORE INSERT ON emp3 BEGIN
4 IF (TO_CHAR (sysdate,'DY') IN ('SAT','SUN')) OR5 (TO_CHAR(sysdate,'HH24') NOT BETWEEN6 '08' AND '18')7 THEN RAISE_APPLICATION_ERROR (-20500,
8 'You may only insert into EMP during business hours.');9 END IF;10 END;
-
8/7/2019 examples on trigger
4/18
Creating Statement Triggers
Using Procedure Builder
Creating Statement Triggers
Using Procedure Builder
-
8/7/2019 examples on trigger
5/18
Using Conditional PredicatesCREATE OR REPLACE TRIGGER secure_empBEFORE INSERT OR UPDATE OR DELETE ON empBEGINIF (TO_CHAR (sysdate,'DY') IN ('SAT','SUN')) OR
(TO_CHAR (sysdate, 'HH24') NOT BETWEEN '08' AND '18')THEN
IF DELETINGTHEN RAISE_APPLICATION_ERROR (-20502,'You may only delete from EMP during normal hours.');ELSIF INSERTINGTHEN RAISE_APPLICATION_ERROR (-20500,
'You may only insert into EMP during normal hours.');ELSIF UPDATING ('SAL')THEN RAISE_APPLICATION_ERROR (-20503,
'You may only update SAL during normal hours.');ELSERAISE_APPLICATION_ERROR (-20504,'You may only update EMP during normal hours.');
END IF;END IF;
END;
-
8/7/2019 examples on trigger
6/18
ExamplesA triger to return back to the first record
if the pointer reach the last record. The trigger on the level of : BLOCK
type : KEY-DOWN
IF :SYSTEM.LAST-RECORD =TRUETHENFIRST-RECORD;
ELSENEXT_RECORD;END IF;
-
8/7/2019 examples on trigger
7/18
Trigger To show the time on a form
trigger on the level of form
type : when _new form_instance
DECLARET TIMER ;BEGINT:=CREATE _TIMER ('TI',1000,REPEAT);END;
there shoulb be another TRIGGER to finish the first :level of TRIGGER : form
type : WHEN_TIMER_EXPIRED
:CLOCK :=TO_CHAR(SYSDATE,'HH:MI:SS');
where the name : CLOCK is DISPLAY ITEM
-
8/7/2019 examples on trigger
8/18
the trigger will maximize the window on run time level of trigger is form
type =when_new_form instance
beginset_window_property(forms_mdi_window,title,' ');set_window_property(forms_mdi_window,window_state,maximize);
set_window_property('window1',window_state,maximize);end;
-
8/7/2019 examples on trigger
9/18
A Trigger To Run A Report From A
Formtype : when_button_pressed
level : on button
declarepl_id number;beginrun_product(reports,'emp.rep',synchronous,runtime,filesystem,
pl_id,null);end;
whereemp =report name
-
8/7/2019 examples on trigger
10/18
: foreign key
: post-query
: block
customers (city_number) foreign key
(master table) cities_table (city_no, city_name)
:
:display item c_name database item = No
post-query
DECLARECURSOR c1 IS SELECT city_name FROM cities_table WHERE city_no = :customers.city_number
BEGINOPEN c1;
FETCH c1 INTO :customers.c_name;IF c1%NOTFOUND THEN
:customers.c_name := 'not available' ;END IF;
CLOSE c1;END;
1)
2) list of valuewhen-validate-itemtrigger
3) when_validate_item : :customers.city_number
4) post-query & when-validate-item :post-change .
-
8/7/2019 examples on trigger
11/18
=inv_d =items
=
= pre_insert= data block
declarea number ;beginselect items.qty into a from item where items.item_no=:inv_d.item_no;
if :inv_d.qty>a thenmessage(' ');raise form_trigger_failure;elseupdate items set qty=qty-:inv_d.qty where item_no=:inv_d.item_no ;end if;end
-
8/7/2019 examples on trigger
12/18
PASING PARAMETER BETWEEN FORMS
DEPT 20 EMP
DEPT F3 PARAMETERS NO PARAMETER DATA TYPENUMBER
DEPTNO . DEPT EMPTABULAR EMP ..
WHEN_BUTTON_PREESED DECLARE
A PARAMLIST;BEGIN
A:=CREATE_PARAMETER_LIST('THA');ADD_PARAMETER(A,'NO',TEXT_ITEM,PARAMETER,:DEPTNO);
RUN_PRODUCT(FORMS,'EMP',ASY
NCHRONOUS,RUNTIME,FILESY
STEM,A,'');EXIT_FORM;END;
..
TRIGEER WHEN_NEW_FORM_INSTANCE :
DECLARE
B BLOCK;BEGINB:=FIND_BLOCK('EMP');
IF(:PARAMETER.NO IS NOT NULL)THENSET_BLOCK_PROPERTY(B,DEFAULT_WHERE,'DEPTNO='||:PARAMETER.NO);
GO_BLOCK('EMP');EXECUTE_QUERY;
END IF;END;
-
8/7/2019 examples on trigger
13/18
exe
host('"c:\winnt\notepad.exe")
-
8/7/2019 examples on trigger
14/18
username/password fmx
. (ON-LOGON)
:
LOGON('SCOTT','TIGER@SERVICE_NAEM');
-
8/7/2019 examples on trigger
15/18
You can create this trigger to monitor how often you log on and off, or you maywant to write a report on how long you are logged on for. When you specify ONSCHEMA,the trigger fires for the specific user. If you specify ON DATABASE, thetrigger fires for all users
-
8/7/2019 examples on trigger
16/18
Sample LOG_TRIG_TABLE:SQL> describe log_trig_tableName Null? Type------------------------------- -------- ----USER_ID VARCHAR2(30)LOG_DATE DATEACTION VARCHAR2(30)
SQL> select * from log_trig_table;
USER_ID LOG_DATE ACTION-------------------------- --------- ---------------------A_USER 23-MAR-99 Logging offA_USER 23-MAR-99 Logging on
-
8/7/2019 examples on trigger
17/18
The following example creates a logon statistics table and a LOGON and LOGOFF databasetrigger to capture the time when a user connects/disconnects to/from the database.
CREATE TABLE session_logon_statistics
(user_logged VARCHAR2(30),start_time DATE,end_time DATE);
CREATE OR REPLACE TRIGGER logon_log_triggerAFTER LOGONON DATABASE
BEGININSERT INTO session_logon_statistics(user_logged, start_time)VALUES(USER, SYSDATE);END;/
CREATE OR REPLACE TRIGGER logoff_log_triggerBEFORE LOGOFFON DATABASEBEGINUPDATE session_logon_statisticsSET end_time = SYSDATEWHERE user_logged = USERAND end_time IS NULL;END
-
8/7/2019 examples on trigger
18/18
The following script retrieves the information from the session_logon_statistics table.
SELECT user_logged,
TO_CHAR(start_time, 'MM/DD/YYYY HH24:MI:SS')"START TIME",
TO_CHAR(end_time, 'MM/DD/YYYY HH24:MI:SS')"END TIME"
FROM session_logon_statistics
order by user_logged, start_time;