-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_procedure.sql
More file actions
65 lines (45 loc) · 1.01 KB
/
Copy pathfunction_procedure.sql
File metadata and controls
65 lines (45 loc) · 1.01 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
-- Functions
-- Create function to calculate average price of books
create function book_avg_price()
returns int
language sql
as
$$
select avg(price) from books
$$
select book_avg_price()
-- Function to delete a book by id
create function book_book(bookId int)
returns void
language sql
as
$$
delete from books where id=bookId
$$
select book_book(17)
-- Procedures
-- Create procedure to delete a book by id
create procedure delete_book_by_id(bookId int)
language plpgsql
as
$$
begin
delete from books where id=bookId;
end;
$$
call delete_book_by_id(6)
-- Create procedure to update stock based on category and stock condition
create procedure update_stock_2(b_category varchar,b_stock int)
language plpgsql
as
$$
declare
total_book int;
begin
select count(stock) into total_book from books
where category = b_category and total_book < b_stock;
update books set stock = stock+100
where category = b_category and total_book < b_stock;
end;
$$
call update_stock_2('Web',30)