-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09_plsql.sql
More file actions
52 lines (48 loc) · 1.32 KB
/
Copy path09_plsql.sql
File metadata and controls
52 lines (48 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
-- ============================================================
-- Purpose : Automatically record order delivery status changes
-- Author : [Your Name]
-- Date : [Submission Date]
-- Tool : Oracle 11g Express Edition
-- ============================================================
CREATE OR REPLACE TRIGGER trg_order_status_history
AFTER UPDATE OF order_status ON customer_order
FOR EACH ROW
WHEN (OLD.order_status <> NEW.order_status)
DECLARE
v_note VARCHAR2(200);
BEGIN
v_note := 'Status changed automatically by trigger';
INSERT INTO order_status_history (
status_history_id,
order_id,
old_status,
new_status,
changed_by,
changed_at,
note
)
VALUES (
status_history_seq.NEXTVAL,
:NEW.order_id,
:OLD.order_status,
:NEW.order_status,
USER,
SYSDATE,
v_note
);
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20001, 'trg_order_status_history failed: ' || SQLERRM);
END;
/
-- Suggested trigger test for screenshots:
-- UPDATE customer_order
-- SET order_status = 'ACCEPTED'
-- WHERE order_id = 7010;
--
-- COMMIT;
--
-- SELECT status_history_id, order_id, old_status, new_status, changed_by, changed_at, note
-- FROM order_status_history
-- WHERE order_id = 7010
-- ORDER BY status_history_id;