-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLCategoryFunction.sql
114 lines (106 loc) · 2.09 KB
/
SQLCategoryFunction.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
-- create new category --
-- create_category(name, id_type)
-- return void
-- CREATE OR REPLACE function (
-- )
-- returns void
-- language plpgsql
-- as $$
-- --Declare
-- --err integer;
-- begin
-- end;$$;
-- create new category --
-- create_category(name, id_type)
-- return void
CREATE OR REPLACE function create_category(
p_name text,
p_id_type int
)
returns void
language plpgsql
as $$
begin
insert into category (name, id_type)
values (p_name, p_id_type);
end;$$;
-- update category --
-- update_category(id_category, name)
-- return void
CREATE OR REPLACE function update_category(
p_id_category int,
p_name text
)
returns void
language plpgsql
as $$
--Declare
--err integer;
begin
update category
set name = p_name
where id_category = p_id_category;
end;$$;
-- delete category --
-- delete_category(id_category)
-- return void
CREATE OR REPLACE function delete_category(
p_id_category int
)
returns void
language plpgsql
as $$
--Declare
--err integer;
begin
delete from category
where id_category = p_id_category;
end;$$;
-- view all category information--
-- select_category(id_type)
-- return table()
CREATE OR REPLACE function select_category(
p_id_type int default null
)
returns table(
err int,
id_category int,
name text,
type text
)
language plpgsql
as $$
-- Declare
-- err integer;
begin
if p_id_type is null then
return query
select 1, c.id_category, c.name, p.name
from category c, product_type p
where c.id_type = p.id_type
order by c.id_type, c.id_category;
else
return query
select 1, c.id_category, c.name, c.name
from category c, product_type p
where c.id_type = p_id_type and c.id_type = p.id_type
order by c.id_category;
end if;
end;$$;
-- view all type information --
-- select_type()
-- return table
CREATE OR REPLACE function select_type()
returns table(
id_type int,
name text
)
language plpgsql
as $$
--Declare
--err integer;
begin
return query
select p.id_type, p.name
from product_type p;
end;$$;