-
Notifications
You must be signed in to change notification settings - Fork 0
/
TAOBANG-QLCUA_HANG_THUC_AN_NHANH.sql
186 lines (150 loc) · 4.35 KB
/
TAOBANG-QLCUA_HANG_THUC_AN_NHANH.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
create database QUAN_LY_CUA_HANG_THUC_AN_NHANH
GO
--
use QUAN_LY_CUA_HANG_THUC_AN_NHANH
select * from HOADON where MABAN='H001' and VITRI='trong nhà'
--
CREATE TABLE DANGNHAP
(
MANV INT NOT NULL,
TENDANGNHAP NVARCHAR(50) NOT NULL,
MATKHAU NVARCHAR(50) NOT NULL,
TENNV NVARCHAR(50),
CHUCVU INT DEFAULT 0, -- 0 QUẢN LÝ; 1 NHÂN VIÊN
CONSTRAINT PK_DANGNHAP
PRIMARY KEY (MANV, TENDANGNHAP)
)
GO
--
CREATE TABLE LOAISP
(
MALOAISP CHAR(5),-- FL001; DL001;
TENLOAISP NVARCHAR(50)
CONSTRAINT PK_LOAISP
PRIMARY KEY(MALOAISP)
)
select * from cthoadon
SELECT * FROM LOAISP
select * from SANPHAM --where MALOAISP= 'mlsp'
select * from SANPHAM where MALOAISP= 'DL001'
CREATE TABLE SANPHAM
(
MASP CHAR(4) NOT NULL, -- F001 HOẶC D001 (F: FOOD; D: DRINK)
TENSP NVARCHAR(50) NOT NULL,
DONGIA INT DEFAULT 1000,
MALOAISP CHAR(5),-- FL001; DL001
CONSTRAINT PK_SANPHAM
PRIMARY KEY (MASP)
)
GO
--
select * from tableinfo
CREATE TABLE TABLEINFO
(
MABAN CHAR(4), --TRONG NHA: H; TANG: F, VIP: V + 001;
VITRI NVARCHAR(20),
TENBAN NVARCHAR(10),
TINHTRANG NVARCHAR(10), -- TRỐNG; CÓ KHÁCH
CONSTRAINT PK_TABLEINFO
PRIMARY KEY (MABAN,VITRI)
)
GO
--
CREATE TABLE HOADON
(
MAHOADON CHAR(5),
CHECKIN DATETIME default getdate(),
CHECKOUT DATETIME null,
MABAN CHAR(4),
VITRI NVARCHAR(20),
TINHTRANG NVARCHAR(20), -- ĐÃ THANH TOÁN, CHƯA THANH TOÁN
GIAMGIA int default 0
CONSTRAINT PK_HOADON
PRIMARY KEY (MAHOADON)
)
CREATE TABLE THONGTINCHITIETHOADON
(
MActHOADON CHAR(5),
MAHOADON CHAR(5),
MASP CHAR(4),
SOLUONG INT,
CONSTRAINT PK_THONGTINCHITIETHOADON
PRIMARY KEY (MActHOADON)
)
select * from THONGTINCHITIETHOADON
alter table HOADON
add GIAMGIA INT
UPDATE HOADON SET GIAMGIA =0
SELECT * FROM HOADON
SELECT * FROM THONGTINCHITIETHOADON -- billInFo
SELECT * FROM SANPHAM
SELECT * FROM LOAISP
select * from TABLEINFO
SELECT * FROM THONGTINCHITIETHOADON where MActHOADON = 'BL001'
-- load table lên flp
create proc USP_loadTable
as select * from TABLEINFO
exec dbo.USP_loadTable
go
--
--thông tin hóa đơn
SELECT SP.TENSP, HD_iF.SOLUONG, SP.DONGIA, SP.DONGIA*HD_iF.SOLUONG AS THANHTIEN
FROM SANPHAM AS SP, HOADON AS HD, THONGTINCHITIETHOADON AS HD_iF, TABLEINFO as TB_IF
WHERE HD.MAHOADON=HD_iF.MAHOADON AND HD_iF.MASP=SP.MASP AND HD.MABAN=TB_IF.MABAN
SELECT HD.VITRI, SP.TENSP, HD_iF.SOLUONG, SP.DONGIA, SP.DONGIA* HD_iF.SOLUONG AS THÀNH_TIỀN FROM SANPHAM AS SP, HOADON AS HD, THONGTINCHITIETHOADON AS HD_iF WHERE HD.MAHOADON = HD_iF.MAHOADON AND HD_iF.MASP = SP.MASP AND HD.MABAN =MABAN AND HD.TINHTRANG= 'Chưa thanh toán'
SELECT hd.VITRI,hd.MABAN,SP.TENSP, HD_iF.SOLUONG, SP.DONGIA, SP.DONGIA* HD_iF.SOLUONG AS THANHTIEN FROM SANPHAM AS SP, HOADON AS HD, THONGTINCHITIETHOADON AS HD_iF WHERE HD.MAHOADON = HD_iF.MAHOADON AND HD_iF.MASP = SP.MASP AND HD.TINHTRANG =N'Chưa thanh toán'
SELECT * FROM LOAISP
select * from THONGTINCHITIETHOADON
select * from hoadon
-- Proc cho nút thêm món ăn. bài 11
create Proc USP_InsertBill
@maHD char(5), @MABAN char(4), @VITRI NVARCHAR(20)
as
begin
declare @tinhtrang nvarchar(20) =N'Chưa thanh toán'
insert HOADON(MAHOADON, CHECKIN, CHECKOUT, MABAN, VITRI, TINHTRANG,GIAMGIA)
VALUES (@maHD, GETDATE(), null, @MABAN, @VITRI, @tinhtrang,0)
END
GO
--
-- Bài 11
--isExitBillInfo = MActHOADON là sai
--update thiếu dữ liệu cho MActHOADON-- nó ko được upd tự động tăng dần
ALTER Proc USP_InsertBillInfo
@CTmaHD char(5), @MAHOADON char(5), @MASP char(4) , @SOLUONG int
as
begin
DECLARE @IsExitBillInfo char(5)
DECLARE @FoodCount int =1
select
@IsExitBillInfo = MActHOADON,
@FoodCount = THONGTINCHITIETHOADON.SOLUONG
FROM THONGTINCHITIETHOADON
WHERE MAHOADON = @MAHOADON AND MASP = @MASP
IF (@IsExitBillInfo is not null)
BEGIN
DECLARE @NEWCOUNT INT = @FoodCount + @SOLUONG
IF(@NEWCOUNT >0)
UPDATE THONGTINCHITIETHOADON SET SOLUONG = @FoodCount + @SOLUONG where MASP = @MASP
ELSE
DELETE THONGTINCHITIETHOADON WHERE MAHOADON = @MAHOADON AND MASP =@MASP
END
ELSE
BEGIN
insert THONGTINCHITIETHOADON
(MActHOADON,
MAHOADON ,
MASP ,
SOLUONG)
VALUES
(@CTmaHD, @MAHOADON, @MASP, @SOLUONG)
END
END
GO
select * from THONGTINCHITIETHOADON
select * from HOADON where MABAN ='F001' and TINHTRANG = 'Chưa thanh toán'
-- getmahoadon() HoadonDAO
select count(*) from HOADON
select * from HOADON
select * from LOAISP
select sp.MASP, sp.tensp from SANPHAM sp where MALOAISP='DL001'