-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspProdSubCategInsertUpdateDelete.sql
95 lines (72 loc) · 2.16 KB
/
spProdSubCategInsertUpdateDelete.sql
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
-- PROCEDURE: public.spprodsubcateginsertupdatedelete(integer, integer, character varying, text, integer)
-- DROP PROCEDURE public.spprodsubcateginsertupdatedelete(integer, integer, character varying, text, integer);
CREATE OR REPLACE PROCEDURE public.spprodsubcateginsertupdatedelete(
_ser integer,
_pcid integer,
_name character varying,
_desc text,
_psid integer DEFAULT NULL::integer)
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
IF _ser=1 THEN -- INSERT
INSERT INTO product_sub_category (prod_category_id, prod_subcateg_name, prod_subcateg_desc)
VALUES (_pcid, _name, _desc);
ELSEIF _ser=2 THEN --UPDATE
UPDATE product_sub_category SET
prod_category_id = _pcid,
prod__subcateg_name = _name,
prod_subcateg_desc = _desc
where prod_subcateg_id = _psid;
ELSEIF _ser=3 THEN --DELETE
IF EXISTS (SELECT 1 FROM product_sub_category a
INNER JOIN product b on a.prod_subcateg_id = b.prod_subcateg_id
WHERE a.prod_subcateg_id = _psid) THEN
BEGIN
RAISE NOTICE 'There are products under this category! Delete them first.';
END;
ELSE
BEGIN
DELETE FROM product_sub_category
WHERE prod_subcateg_id = _psid;
END;
END IF;
END IF;
END
$BODY$;
/* Commented on 12 Nov 20------------------
CREATE OR REPLACE PROCEDURE spProdSubCategInsertUpdateDelete(
_ser INTEGER,
_pcid INTEGER,
_name VARCHAR(50),
_desc TEXT,
_psid INTEGER DEFAULT NULL
)
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
IF _ser=1 THEN -- INSERT
INSERT INTO product_sub_category (prod_category_id, prod_name, prod_desc)
VALUES (_pcid, _name, _desc);
ELSEIF _ser=2 THEN --UPDATE
UPDATE product_sub_category SET
prod_category_id = _pcid,
prod_name = _name,
prod_desc = _desc
where prod_subcateg_id = _psid;
ELSEIF _ser=3 THEN --DELETE
IF EXISTS (SELECT 1 FROM product_sub_category a
INNER JOIN product b on a.prod_subcateg_id = b.prod_subcateg_id
WHERE a.prod_subcateg_id = _psid) THEN
BEGIN
RAISE NOTICE 'There are products under this category! Delete them first.';
END;
ELSE
BEGIN
DELETE FROM product_sub_category
WHERE prod_subcateg_id = _psid;
END;
END IF;
END IF;
END
$BODY$;