-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriggers.sql
More file actions
62 lines (46 loc) · 1.31 KB
/
triggers.sql
File metadata and controls
62 lines (46 loc) · 1.31 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
52
53
54
55
56
57
58
59
60
61
62
/*Trigger for storing the change in discounts of a product*/
CREATE TABLE discount_log (
pid VARCHAR(9),
New_discount NUMBER,
Old_discount NUMBER,
Log_date DATE
);
create or replace TRIGGER Log_discount_changes
AFTER UPDATE OF discount ON product
FOR EACH ROW
BEGIN
INSERT INTO discount_log (pid, New_discount, Old_discount, Log_Date)
VALUES (:new.pid, :new.discount, :old.discount, SYSDATE);
END;
/* stock change log*/
CREATE TABLE stock_log (
stockid VARCHAR(9),
New_stock NUMBER,
Old_stock NUMBER,
Log_date DATE
);
create or replace TRIGGER Log_stock_changes
AFTER UPDATE OF quantity ON stock
FOR EACH ROW
BEGIN
INSERT INTO discount_log
VALUES (:new.stockid, :new.quantity, :old.quantity, SYSDATE);
END;
/*stored procedures */
/*set discount as 50 when discount>50*/
create or replace PROCEDURE Decrease_discount AS
thisProduct Product%ROWTYPE;
CURSOR discount_50 IS
SELECT * FROM product where discount>50 FOR UPDATE;
BEGIN
OPEN discount_50;
LOOP
FETCH discount_50 INTO thisProduct;
EXIT WHEN (discount_50%NOTFOUND);
dbms_output.put_line(thisProduct.pid);
dbms_output.put_line(thisProduct.discount);
UPDATE product SET discount = 50
WHERE CURRENT OF discount_50;
END LOOP;
CLOSE discount_50;
END;