Oracle PL / SQL - Пример триггера после INSERT
В этой статье показано, как использоватьAFTER INSERT TRIGGER, он срабатывает после выполнения операции вставки.
1. После триггера INSERT
В этом примере, если новый пользователь создается вuser_details, но отсутствуют такие поля, какpassport_no илиdriving_license_no, новая запись будет вставлена вuser_reminders через 'после вставки 'запуск наuser_details
1.1 Create tables and trigger.
USER_DETAILS
CREATE TABLE USER_DETAILS
(
USER_ID number(10) primary key,
USER_NAME varchar2(15),
EMAIL varchar2(20),
PHONE number(12),
PASSPORT_NO varchar2(10),
DRIVING_LICENSE_NO varchar2(10)
);
USER_REMINDERS
CREATE TABLE USER_REMINDERS
(
USER_ID number(10),
REMINDER_TEXT varchar2(200),
REMINDER_DATE date,
STATUS varchar2(10)
);
trg_after_insert_user
CREATE OR REPLACE TRIGGER trg_after_insert_user
AFTER INSERT
on USER_DETAILS
FOR EACH ROW
DECLARE
counter number(2);
reminder_text varchar2(200);
BEGIN
counter := 0;
reminder_text := '';
IF(:NEW.PASSPORT_NO = '' OR :NEW.PASSPORT_NO is null) THEN
reminder_text := 'Please insert your passport details into system. ';
counter := counter+1;
END IF;
IF(:NEW.DRIVING_LICENSE_NO = '' OR :NEW.DRIVING_LICENSE_NO is null) THEN
reminder_text := reminder_text || 'Please insert your Driving license details into system.';
counter := counter+1;
END IF;
-- If passport_no and/or driving_license_no is missing
-- then counter will be >0 and below code will insert into user_reminders table.
IF(counter>0) THEN
INSERT INTO USER_REMINDERS VALUES (:NEW.USER_ID,reminder_text,sysdate+3,'PENDING');
END IF;
END;
1.2 Insert data to test the trigger.
-- fire after insert trigger, no action. INSERT INTO USER_DETAILS VALUES (1,'USERNM1','[email protected]',9999999999,'PASSNUM123','DRIVLIC999'); -- fire after insert trigger, password is null, insert new record into USER_REMINDERS INSERT INTO USER_DETAILS VALUES (2,'USERNM22','[email protected]',1111111111,null,'LICNC12345'); -- fire after insert trigger, password and driving no are null, insert new record into USER_REMINDERS INSERT INTO USER_DETAILS VALUES (3,'USERNM33','[email protected]',3333333333,null,null); -- fire after insert trigger, driving no is null, insert new record into USER_REMINDERS INSERT INTO USER_DETAILS VALUES (4,'USERNM44','[email protected]',4444444444,'ONLYPASS11',null);
1.3 Display the data.
select * from USER_DETAILS;
| ИДЕНТИФИКАТОР ПОЛЬЗОВАТЕЛЯ | USER_NAME | ЭЛ. АДРЕС | ТЕЛЕФОН | ПАСПОРТ № | DRIVING_LICENSE_NO |
|---|---|---|---|---|---|
1 |
USERNM1 |
9999999999 |
ПАССА123 |
DRIVLIC999 |
|
2 |
USERNM22 |
1111111111 |
null |
LICNC12345 |
|
3 |
USERNM33 |
3333333333 |
null |
null |
|
4 |
USERNM44 |
4444444444 |
ONLYPASS11 |
null |
select * from USER_REMINDERS;
| ИДЕНТИФИКАТОР ПОЛЬЗОВАТЕЛЯ | REMINDER_TEXT | REMINDER_DATE | ПОЛОЖЕНИЕ ДЕЛ |
|---|---|---|---|
2 |
Пожалуйста, введите свои паспортные данные в систему. |
21-ИЮН-2017 |
ОЖИДАНИЕ |
3 |
Пожалуйста, введите ваши паспортные данные в систему. |
21-ИЮН-2017 |
ОЖИДАНИЕ |
4 |
Пожалуйста, введите данные ваших водительских прав в систему. |
21-ИЮН-2017 |
ОЖИДАНИЕ |