-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstored-procedures.sql
467 lines (399 loc) · 9.1 KB
/
stored-procedures.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
-----------------------
-- STORED PROCEDURES --
-----------------------
USE Proj_A10
GO
---------------------------
-- Creator: Elisa Truong --
---------------------------
-- INSERT PROCEDURES
CREATE PROCEDURE insertCartItem
@Fname VARCHAR(50),
@Lname VARCHAR(50),
@DOB DATE,
@Gender VARCHAR(50),
@Game_Name VARCHAR(50),
@Plat_Name VARCHAR(50),
@Quantity INT
AS
DECLARE @Game_ID INT, @Plat_ID INT, @Cust_ID INT
EXEC getGamerID
@G_Fname = @Fname,
@G_Lname = @Lname,
@G_Gender = @Gender,
@G_DOB = @DOB,
@G_ID = @Cust_ID OUTPUT
IF @Cust_ID IS NULL
BEGIN
RAISERROR('Gamer ID cannot be null!', 11, 1)
RETURN
END
EXEC getGameID
@GName = @Game_Name,
@GID = @Game_ID OUTPUT
IF @Game_ID IS NULL
BEGIN
RAISERROR('Game ID cannot be null!', 11, 1)
RETURN
END
EXEC getPlatformID
@PName = @Plat_Name,
@PID = @Plat_ID OUTPUT
IF @Plat_ID IS NULL
BEGIN
RAISERROR('Platform ID cannot be null', 11, 1)
RETURN
END
DECLARE @Price MONEY = (SELECT gp.CurrentPrice FROM tblGamePlatform AS gp
WHERE gp.GameID = @Game_ID AND
gp.PlatformID = @Plat_ID)
IF @Price IS NULL
BEGIN
RAISERROR('Price of game cannot be null', 11, 1)
RETURN
END
DECLARE @Subprice MONEY = @Price * @Quantity
BEGIN TRAN addItemToCart
INSERT INTO tblCART(GamerID, GameID, PlatformID, CartQty, CartSubprice, GamePrice)
VALUES (@Cust_ID, @Game_ID, @Plat_ID, @Quantity, @Subprice, @Price)
IF @@ERROR<> 0
BEGIN
PRINT('Ran into error while adding item to cart!')
ROLLBACK TRAN addItemToCart
END
ELSE
COMMIT TRAN addItemToCart
GO
-- PROCESS CART PROCEDURE
CREATE PROCEDURE processCart
@Fname VARCHAR(50),
@Lname VARCHAR(50),
@DOB DATE,
@Gender VARCHAR(50),
@PurchaseDate DATE
AS
DECLARE @Cust_ID INT
EXEC getGamerID
@G_Fname = @Fname,
@G_Lname = @Lname,
@G_Gender = @Gender,
@G_DOB = @DOB,
@G_ID = @Cust_ID OUTPUT
IF @Cust_ID IS NULL
BEGIN
RAISERROR('Gamer ID cannot be null!', 11, 1)
RETURN
END
DECLARE @OrderSum MONEY = (SELECT SUM(CartSubprice) FROM tblCART WHERE GamerID = @Cust_ID)
BEGIN TRAN insertOrder
INSERT INTO tblORDER(GamerID, OrderDate, OrderTotal)
VALUES (@Cust_ID, @PurchaseDate, @OrderSum)
DECLARE @Order_ID INT = (SELECT SCOPE_IDENTITY())
IF @Order_ID IS NULL
BEGIN
RAISERROR('Order ID cannot be null!', 11, 1)
RETURN
END
INSERT INTO tblORDER_GAME (OrderID, PlatformID, GameID, OrderGameQty, OrderGameSubprice, GamePrice)
SELECT @Order_ID, PlatformID, GameID, SUM(CartQty), SUM(CartSubprice), GamePrice
FROM tblCART
WHERE GamerID = @Cust_ID
GROUP BY PlatformID, GameID, GamePrice
IF @@ERROR <> 0
BEGIN
PRINT('Error inserting Order Game content')
ROLLBACK TRAN insertOrder
END
ELSE
COMMIT TRAN insertOrder
DECLARE @SuccessMsg VARCHAR(100) = (SELECT CONCAT('Successfully inserted information for ', @FName, ' ', @LName))
PRINT(@SuccessMsg)
DELETE FROM tblCART
WHERE GamerID = @Cust_ID
GO
---------------------------
-- Creator: Marcus Huang --
---------------------------
-- tblGAME: InsertGame
CREATE PROCEDURE insertGame
@GName varchar(50),
@GReleaseDate DATE,
@GDescription varchar(100),
@PRateName varchar(30),
@PerName varchar(30),
@GeTName varchar(30)
AS
DECLARE @GT_ID INT, @P_ID INT, @PR_ID INT
EXEC getGenreTypeID
@GTypeName = @GeTName,
@GType_ID = @GT_ID OUTPUT
IF @GT_ID IS NULL
BEGIN
RAISERROR('GenreTypeID cannot be null', 11,1)
RETURN
END
EXEC getParentRateID
@PRateName = @PrateName,
@PRate_ID = @PR_ID OUTPUT
IF @PR_ID IS NULL
BEGIN
RAISERROR('ParentRateID cannot be null', 11,1)
RETURN
END
EXEC getPerspective
@PerName = @PerName,
@PerID = @P_ID OUTPUT
IF @P_ID IS NULL
BEGIN
RAISERROR('PerspectiveID cannot be null', 11,1)
RETURN
END
BEGIN TRAN addGame
INSERT INTO tblGAME(GameName, GenreTypeID, GameReleaseDate, GameDescription, PerpID, ParentRateID)
VALUES (@GName, @GT_ID, @GReleaseDate, @GDescription, @P_ID, @PR_ID)
IF @@ERROR<> 0
BEGIN
PRINT('Ran into error while inserting a game')
ROLLBACK TRAN addGame
END
ELSE
COMMIT TRAN addGame
GO
-- tblREVIEW: InsertReview
CREATE PROCEDURE insertREVIEW
@RRating float,
@RContent varchar(255),
@RDate DATE,
@GameName varchar(50),
@GamerFname varchar(50),
@GamerLname varchar(50),
@GamerBday DATE,
@OrderDate DATE,
@PlatformName varchar(50),
@Gender varchar(50),
@OTotal numeric(5,2)
AS
DECLARE @OG_ID INT
EXEC getOrderGameID
@GName = @GameName,
@PName = @PlatformName,
@FName = @GamerFname,
@LName = @GamerLname,
@Gender = @Gender,
@BDate = @GamerBday,
@ODate = @OrderDate,
@OTotal = @OTotal,
@OGID = @OG_ID OUTPUT
IF @OG_ID IS NULL
BEGIN
RAISERROR('OrderGameID cannot be null', 11,1)
RETURN
END
BEGIN TRAN addReview
INSERT INTO tblREVIEW(OrderGameID, ReviewRating, ReviewContent, ReviewDate)
VALUES (@OG_ID, @RRating, @RContent, @RDate)
IF @@ERROR<> 0
BEGIN
PRINT('Ran into error while inserting a game')
ROLLBACK TRAN addReview
END
ELSE
COMMIT TRAN addReview
GO
------------------------
-- Creator: Angel Lin --
------------------------
-- Insert Procedure: tblPlatformPriceHistory --
CREATE PROCEDURE getGamePlatformID
@GN VARCHAR(50),
@PN VARCHAR(50),
@GPID INT OUTPUT
As
DECLARE @G_ID INT, @P_ID INT
EXEC getGameID
@GName = @GN,
@GID = @G_ID OUTPUT
IF @G_ID IS NULL
BEGIN
RAISERROR('Game ID cannot be null!', 11, 1)
RETURN
END
EXEC getPlatformID
@PName = @PN,
@PID = @P_ID OUTPUT
IF @P_ID IS NULL
BEGIN
RAISERROR('Platform ID cannot be null!', 11, 1)
RETURN
END
SET @GPID = (SELECT GamePlatformID FROM tblGamePlatform
WHERE GameID = @G_ID
AND PlatformID = @P_ID)
GO
CREATE PROCEDURE insPlatformPriceHistory
@G_Name VARCHAR(50),
@P_Name VARCHAR(50),
@H_Price MONEY,
@H_SDate DATE,
@H_EDate DATE
AS
DECLARE @GP_ID INT
EXEC getGamePlatformID
@GN = @G_Name,
@PN = @P_Name,
@GPID = @GP_ID OUTPUT
IF @GP_ID IS NULL
BEGIN
RAISERROR('Game Platform ID cannot be null!', 11, 1)
RETURN
END
BEGIN TRAN addPlatformPriceHistory
INSERT INTO tblPlatform_Price_History(GamePlatformID, HistoryPrice, HistoryStart, HistoryEnd)
VALUES (@GP_ID, @H_Price, @H_SDate, @H_EDate)
IF @@ERROR<> 0
BEGIN
PRINT('Ran into error while adding item to platform price history!')
ROLLBACK TRAN addPlatformPriceHistory
END
ELSE
COMMIT TRAN addPlatformPriceHistory
GO
-- Insert Procedure: tblGamePlatform --
CREATE PROCEDURE insGamePlatform
@GN VARCHAR(50),
@PN VARCHAR(50),
@ReleaseDate DATE
AS
DECLARE @G_ID INT, @P_ID INT
EXEC getGameID
@GName = @GN,
@GID = @G_ID OUTPUT
IF @G_ID IS NULL
BEGIN
RAISERROR('Game ID cannot be null!', 11, 1)
RETURN
END
EXEC getPlatformID
@PName = @PN,
@PID = @P_ID OUTPUT
IF @P_ID IS NULL
BEGIN
RAISERROR('Platform ID cannot be null!', 11, 1)
RETURN
END
BEGIN TRAN addToGamePlatform
INSERT INTO tblGamePlatform(GameID, PlatformID, PlatformReleaseDate)
VALUES (@G_ID, @P_ID, @ReleaseDate)
IF @@ERROR <> 0
BEGIN
PRINT('Ran into error while adding item to Game Platform!')
ROLLBACK TRAN addToGamePlatform
END
ELSE
COMMIT TRAN addToGamePlatform
GO
-----------------------
-- Creator: Andi Ren --
-----------------------
CREATE PROCEDURE InsertGamer
@Fname VARCHAR(50),
@Lname VARCHAR(50),
@DOB DATE,
@Username VARCHAR(50),
@GenderName VARCHAR(50)
AS
DECLARE @Gender_ID INT = (SELECT GenderID FROM tblGENDER WHERE GenderName = @GenderName)
IF @Gender_ID IS NULL
BEGIN
RAISERROR('Gender ID cannot be null! Please input correct gender name', 11, 1)
RETURN
END
BEGIN TRAN g1
Insert into tblGAMER (GamerFname, GamerLname, GamerDOB, GamerUsername, GenderID)
VALUES (
@Fname, @Lname, @DOB, @Username, @Gender_ID
)
IF @@ERROR <> 0
BEGIN
PRINT('Error inserting gamer interest row')
ROLLBACK TRAN g1
END
ELSE
COMMIT TRAN g1
GO
CREATE PROCEDURE InsertGAMER_INTEREST
@Fname VARCHAR(50),
@Lname VARCHAR(50),
@DOB DATE,
@Gender VARCHAR(50),
@Keyword VARCHAR(50)
AS
DECLARE @Gamer_ID INT, @Keyword_ID INT
EXEC getGamerID
@G_Fname = @Fname,
@G_Lname = @Lname,
@G_Gender = @Gender,
@G_DOB = @DOB,
@G_ID = @Gamer_ID OUTPUT
IF @Gamer_ID IS NULL
BEGIN
RAISERROR('Gamer ID cannot be null!', 11, 1)
RETURN
END
EXEC getKeywordID
@KeyName = @Keyword,
@KeyID = @Keyword_ID OUTPUT
IF @Keyword_ID IS NULL
BEGIN
RAISERROR('Keyword ID cannot be null!', 11, 1)
RETURN
END
BEGIN TRAN g1
Insert into tblGAMER_INTEREST(GamerID, KeywordID)
VALUES (
@Gamer_ID, @Keyword_ID
)
IF @@ERROR <> 0
BEGIN
PRINT('Error inserting gamer interest row')
ROLLBACK TRAN g1
END
ELSE
COMMIT TRAN g1
GO
CREATE PROCEDURE InsertDEVELOPER_GAME
@DevName VARCHAR(50),
@GName VARCHAR(50)
AS
DECLARE @GID INT, @DID INT
EXEC getGameID
@GName = @GName,
@GID = @GID OUTPUT
IF @GID IS NULL
BEGIN
RAISERROR('Game ID is null', 11, 1)
RETURN
END
EXEC GetDeveloperID
@DevName = @DevName,
@DevID = @DID OUTPUT
IF @DID IS NULL
BEGIN
RAISERROR('Developer ID is null', 11, 1)
RETURN
END
BEGIN TRAN g1
INSERT INTO tblDEVELOPER_GAME
(DeveloperID,
GameID)
VALUES
(@DID,
@GID)
IF @@ERROR <> 0
BEGIN
PRINT('Error inserting developer game row')
ROLLBACK TRAN g1
END
ELSE
COMMIT TRAN g1
GO