-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKHProcedure1.sql
195 lines (176 loc) · 4.4 KB
/
KHProcedure1.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use Bookstore;
----------------
----------------------------------------
drop proc if exists updateInfo_Customer;
go
create proc updateInfo_Customer
(
@cid int,
@caddress varchar(30),
@cname varchar(30),
@cemail varchar(20)
)
as
begin
update customer
SET
caddress = @caddress,
cname = @cname,
cemail = @cemail
WHERE cid = @cid;
end;
go
exec updateInfo_Customer @cid = 1, @caddress = 'abcxyz', @cname = 'thanh', @cemail = 'thanh@gmail.com';
----------------------------------------
drop proc if exists updateBill_Customer;
go
create proc updateBill_Customer
(
@bbid int,
@quantity int,
@payment varchar(9),
@issue varchar(100),
@price int,
@purchase_date date
)
as
begin
print 'bbid ' + CONVERT(varchar(10), @bbid)
print @purchase_date
update bill
SET
quantity = @quantity,
payment = @payment,
issue = @issue,
price = @price,
purchase_date = @purchase_date
WHERE bbid = @bbid;
end;
go
declare @date_now date;
set @date_now = getdate();
print @date_now
exec updateBill_Customer @bbid = 1, @quantity = 10, @payment = 'bank', @issue = 'failed', @price = 50, @purchase_date = @date_now;
----------------------------------------
drop function if exists list_book_genre;
go
create function list_book_genre
(
@genre varchar(15)
)
returns table
as
return
select *
from book_isbn
where genre = @genre or @genre = 'null';
go
select * from list_book_genre('action')
-----------------------------------------
drop function if exists list_book_author;
go
create function list_book_author
(
@aname varchar(30)
)
returns table
as
return
select book_isbn.isbn, book_isbn.genre, book_isbn.title, book_isbn.price
from (author inner join write on author.aid = write.aid) inner join book_isbn on book_isbn.isbn = write.isbn
where author.aname = @aname or @aname = 'null';
go
select * from list_book_author('a')
------------------------------------------
drop function if exists list_book_keyword;
go
create function list_book_keyword
(
@keyword varchar(15)
)
returns table
as
return
select book_isbn.isbn, book_isbn.genre, book_isbn.title, book_isbn.price
from book_prop inner join book_isbn on book_isbn.isbn = book_prop.isbn
where book_prop.keyword = @keyword or @keyword = 'null';
go
select * from list_book_keyword('Einstein')
-------------------------------------------------
drop function if exists list_book_date_published;
go
create function list_book_date_published
(
@date_published date
)
returns table
as
return
select book_isbn.isbn, book_isbn.genre, book_isbn.title, book_isbn.price
from (author inner join write on author.aid = write.aid) inner join book_isbn on book_isbn.isbn = write.isbn
where write.date_published = @date_published or @date_published is null
go
declare @date_now date;
set @date_now = getdate();
select * from list_book_date_published(@date_now);
---------------------------------------
drop function if exists list_buy_month;
go
create function list_buy_month
(
@cid int,
@date_now date
)
returns @res table(isbn char(20), genre varchar(15), title varchar(20), price int)
as
begin
insert @res
select book_isbn.isbn, book_isbn.genre, book_isbn.title, book_isbn.price
from ebuy
inner join bill on ebuy.bbid = bill.bbid
inner join ebook on ebook.bid = ebuy.bid
inner join book_isbn on ebook.isbn = book_isbn.isbn
where ebuy.cid = @cid and (DATEDIFF(MONTH, bill.purchase_date, @date_now) <= 1);
insert @res
select book_isbn.isbn, book_isbn.genre, book_isbn.title, book_isbn.price
from pbuy
inner join bill on pbuy.bbid = bill.bbid
inner join pbook on pbook.bid = pbuy.bid
inner join book_isbn on pbook.isbn = book_isbn.isbn
where pbuy.cid = @cid and (DATEDIFF(MONTH, bill.purchase_date, @date_now) <= 1);
return
end
go
declare @date_now date;
set @date_now = getdate();
select * from list_buy_month(8, @date_now);
print DATEDIFF(MONTH, cast('1/12/2000' as datetime), @date_now)
---------------------------------
drop function if exists get_book;
go
create function get_book(
@genre varchar(20),
@aname varchar(20),
@keyword varchar(20),
@date_published date
--@date_now date
)
returns @res table(isbn char(20), genre varchar(15), title varchar(20), price int)
as
begin
insert @res
select *
from list_book_author(@aname)
intersect
select *
from list_book_genre(@genre)
intersect
select *
from list_book_keyword(@keyword)
intersect
select *
from list_book_date_published(@date_published)
return
end
go
select * from get_book('science', 'a', 'null', null);