-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMigrationScript.sql
1775 lines (1419 loc) · 36.6 KB
/
MigrationScript.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
USE MASTER
GO
--DROP DATABASE DBMigrations
:setvar DbName DBMigrations
:setvar User DBSkyUser30
--
print('$(DbName)')
DECLARE @DBPrimaryFile varchar(max), @DBLogFile varchar(max)
SET @DBLogFile='$(DbName)'+'.Log'
print(@DBLogFile)
DECLARE @data_path nvarchar(256);
SET @data_path = (SELECT SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1)
FROM master.sys.master_files
WHERE database_id = 1 AND file_id = 1)
EXEC('CREATE DATABASE '+'$(DbName)'+'
ON
PRIMARY
(NAME = ['+'$(DbName)'+'],
FILENAME = '''+ @data_path+ '$(DbName)'+'.mdf'',
SIZE = 6MB,
MAXSIZE = 30MB,
FILEGROWTH = 12%)
LOG ON
(NAME ='+'$(DBName)'+'Log' + ',
FILENAME = '''+ @data_path+'$(DbName)'+'.ldf'',
SIZE = 3MB,
MAXSIZE = 22MB,
FILEGROWTH = 17%)')
GO
Use $(DbName)
GO
EXECUTE('Use '+'$(DbName)'+ '
CREATE TABLE [dbo].[User](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](20) NOT NULL,
[Username] [varchar](20) NOT NULL,
[DOB] [date] NULL,
[Email] [varchar](50) NOT NULL,
[PasswordHash] [varchar](20) NOT NULL,
[PasswordSalt] [varchar](50),
[PhotoUrl] [nvarchar](500) NULL,
Primary Key(ID)
)')
EXECUTE('Use '+'$(DbName)'+'
CREATE TABLE [dbo].[Buyer](
[ID] [int] NOT NULL,
PRIMARY KEY(ID),
FOREIGN KEY(ID) references [User](ID)
On UPDATE cascade
On DELETE cascade
)'
)
EXEC('Use '+'$(DbName)'+'
CREATE TABLE [dbo].[Seller](
[ID] [int] NOT NULL,
PRIMARY KEY(ID),
FOREIGN KEY(ID) references [User](ID)
On UPDATE cascade
On DELETE cascade)'
)
EXEC('Use '+'$(DbName)'+' CREATE TABLE [dbo].[Address](
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserID] [int] NOT NULL,
[Street] [nvarchar](20) NOT NULL,
[City] [nvarchar](20) NOT NULL,
[State] [nvarchar](10) NOT NULL,
[Zip] [nvarchar](5) NOT NULL,
[CampusMailbox] [nvarchar](4),
[status] [nvarchar](20)
PRIMARY KEY(ID)
FOREIGN KEY(UserID) references [User](ID)
On UPDATE cascade
On DELETE cascade)'
)
EXEC('Use ' +'$(DbName)'+
' CREATE TABLE [dbo].[Items](
[ID] [int] IDENTITY(1,1) NOT NULL,
[SellerID] [int] NOT NULL,
[Name] [nvarchar](20) NOT NULL,
[Keyword] [nvarchar](20),
[Type] [nvarchar](20) NOT NULL,
[Description] [nvarchar](200),
[Price] [money] NOT NULL,
[Status] [varchar](20) NOT NULL,
[photoURL] [varchar](500),
[condition] [varchar](100),
primary key(ID),
FOREIGN KEY(SellerID) references Seller(ID)
On UPDATE cascade
On DELETE cascade)'
)
EXEC('Use ' +'$(DbName)'+
' CREATE TABLE [dbo].[CreditCard](
[CardNum] [nvarchar](32) NOT NULL,
[Bank] [nvarchar](20),
[ExpDate] [date] ,
PRIMARY KEY(CardNum)
)'
)
EXEC('Use ' +'$(DBName)'+' CREATE TABLE [dbo].[PaymentMethod](
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserID] [int] NOT NULL,
[Type] [nvarchar](30) ,
[status] [nvarchar](20),
PRIMARY KEY(ID),
FOREIGN KEY (UserID) references [User](ID)
On UPDATE cascade
On DELETE cascade
)'
)
EXEC('Use ' +'$(DBName)'+' CREATE TABLE [dbo].[Transaction](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Location] [nvarchar](20),
[Price] [money],
[Datetime] [date],
[AddressID] [int],
PRIMARY KEY(ID),
FOREIGN KEY(AddressID) references [Address](ID)
ON UPDATE CASCADE
ON DELETE SET NULL
)')
EXEC('Use ' +'$(DBName)'+' CREATE TABLE [dbo].[Has](
[PaymentID] [int] NOT NULL,
[CardNum] [nvarchar](32) NOT NULL,
PRIMARY KEY(PaymentID, CardNum),
FOREIGN KEY(PaymentID) references PaymentMethod(ID)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY(CardNum) references CreditCard(CardNum)
ON UPDATE CASCADE
ON DELETE CASCADE
)')
EXEC('Use '+'$(DBName)'+' CREATE TABLE [dbo].[Shipping](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ShipDate] [date] ,
[TrackingNumber] [nvarchar](20),
[ShippingMethod] [nvarchar](20),
PRIMARY KEY(ID)
)')
EXEC('Use '+'$(DBName)'+ '
CREATE TABLE [dbo].[Buy](
[TransactionID] [int] NOT NULL,
[ItemID] [int] NOT NULL,
[BuyerID] [int] NOT NULL,
[ShippingID] [int],
[PaymentMethodID] [int] NOT NULL,
PRIMARY KEY(TransactionID, ItemID, BuyerID),
FOREIGN KEY (TransactionID) references [Transaction](ID),
FOREIGN KEY(ItemID) references Items(ID),
FOREIGN KEY(BuyerID) references Buyer(ID)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY(ShippingID) references Shipping(ID),
FOREIGN KEY(PaymentMethodID) references PaymentMethod(ID)
)')
EXEC('Use '+'$(DBName)'+' CREATE TABLE [dbo].[SAVE](
[BuyerID] [int] NOT NULL,
[ItemID] [int] NOT NULL,
PRIMARY KEY(BuyerID, ItemID),
FOREIGN KEY(BuyerID) references Buyer(ID),
FOREIGN KEY(ItemID) references Items(ID)
ON UPDATE CASCADE
ON DELETE CASCADE
)'
)
GO
Use $(DbName)
GO
---store procedures
CREATE PROCEDURE [dbo].[AddAdress]
(
@UserID int,
@Street nvarchar(40),
@City nvarchar(40),
@State nvarchar(20),
@Zip nvarchar(10),
@CampusMailBox nvarchar(8),
@Status nvarchar(10)='active',
@AddressID int output
)
AS
BEGIN
IF(@UserID IS NULL)
BEGIN
Raiserror('UserID cannot be null',14,1 )
return 1
END
IF(NOT EXISTS (SELECT * FROM [User] WHERE ID=@UserID))
BEGIN
Raiserror('User does not exist',15,2 )
return 2
END
IF(@Street IS NULL)
BEGIN
Raiserror('Street cannot be null',14,3)
return 3
END
IF(@City IS NULL)
BEGIN
Raiserror('City cannot be null',14,4)
return 4
END
IF(@State IS NULL)
BEGIN
Raiserror('State cannot be null',14,5)
return 5
END
IF(@Zip IS NULL)
BEGIN
Raiserror('Zipcode cannot be null',14,6)
return 6
END
IF(EXISTS(SELECT * FROM [ADDRESS] WHERE UserID=@UserID AND Street=@Street AND City=@City AND [State]=@State AND Zip=@Zip AND CampusMailbox=@CampusMailBox))
BEGIN
UPDATE [ADDRESS]
Set [status]='active'
WHERE UserID=@UserID AND Street=@Street AND City=@City AND [State]=@State AND Zip=@Zip AND CampusMailBox=@CampusMailBox
END
ELSE
BEGIN
INSERT INTO [Address]
(UserID, Street, City, [State], Zip, CampusMailbox, [Status])
VALUES (@UserID, @Street, @City, @State, @Zip, @CampusMailBox, @Status)
END
DECLARE @newAddressID int;
SET @newAddressID = @@IDENTITY;
SET @AddressID = @newAddressID;
return 0
END
GO
Use $(DbName)
GO
CREATE Procedure [dbo].[AddCreditCard](
@CardNum nvarchar(32),
@Bank_1 nvarchar(32),
@ExpirationDate date,
@PaymentMethodID int
)
AS
BEGIN
IF(@PaymentMethodID IS NULL OR NOT EXISTS(SELECT * FROM PaymentMethod Where ID=@PaymentMethodID))
BEGIN
Raiserror('PaymentMethodID is null or invald paymentMethodID', 14, 1)
END
IF(@CardNum IS NULL)
BEGIN
Raiserror('Card Number cannot be null', 14, 2)
return 1
END
IF(@ExpirationDate IS NULL)
BEGIN
Raiserror('Expiration Date cannot be null',14,4)
return 3
END
IF(@ExpirationDate<=(SELECT CONVERT(date, getdate())))
BEGIN
Raiserror('Credit Card is expired', 14, 5)
return 4
END
---add the credit card
INSERT INTO [CreditCard]
(CardNum, Bank, ExpDate)
VALUES (@CardNum, @Bank_1, @ExpirationDate)
IF (@@error <> 0)
BEGIN
ROLLBACK TRANSACTION
return 6
END
---associates the credit card with a payment method
INSERT INTO Has
(PaymentID, CardNum)
VALUES(@PaymentMethodID, @CardNum)
IF (@@error <> 0)
BEGIN
ROLLBACK TRANSACTION
return 7
END
Return 0
END
GO
Create Procedure [dbo].[addItemToSell](
@SellerID Int,
@Name Nvarchar(40),
@Keyword Nvarchar(40)='generic',
@Type Nvarchar(40)='default',
@Description Nvarchar(400)='This is an item',
@Price Money,
@Photourl Nvarchar(200) = null,
@ItemID int output
)
AS
BEGIN TRANSACTION
BEGIN
If( @SellerID is NULL OR Not Exists(SELECT * FROM [USER] Where ID=@SellerID ))
Begin
RAISERROR('Seller ID is null or does not exist ', 14, 1);
ROLLBACK TRANSACTION
return 1
End
If( Not Exists(SELECT * FROM Seller Where ID=@SellerID) and Exists(SELECT * FROM [USER] Where ID=@SellerID ))
Begin
INSERT INTO [SELLER](ID) VALUES (@SellerID)
End
if(@Price is NULL OR @Price<0)
BEGIN
RAISERROR('Price is null or is negative', 14, 2);
ROLLBACK TRANSACTION
return 2
END
if(@Name is NULL)
BEGIN
ROLLBACK TRANSACTION
RAISERROR('Name cannot be null', 14, 3);
return 3
END
INSERT INTO [Items]
( SellerID, [Name], Keyword ,[Type],[Description], Price, photoURL, [Status])
VALUES ( @SellerID,@Name, @Keyword, @Type, @Description, @Price, @Photourl, 'unsold')
DECLARE @newItemID int;
SET @newItemID = @@IDENTITY;
SET @ItemID = @newItemID;
COMMIT TRANSACTION
Return 0
END
GO
CREATE Procedure [dbo].[AddPaymentMethod] (
@UserID int,
@Type nvarchar(60),
@CardNumber nvarchar(32)=null,
@Bank nvarchar(40)=null,
@ExpDate date=null
)
AS
BEGIN
If(@UserID IS NUll OR Not Exists(SELECT * FROM [User] WHERE ID=@UserID))
BEGIN
Raiserror('User is null or does not exist',14, 1)
return 1
END
If(@Type IS NULL)
BEGIN
Raiserror('Payment type cannot be null', 14, 2)
return 2
END
IF(@Type!='Card')
BEGIN
IF(EXISTS(SELECT * FROM PaymentMethod WHERE UserID=@UserID AND [Type]=@Type AND [status]='active'))
BEGIN
Raiserror('Payment method already exists',14,3)
return 2
END
ELSE IF(EXISTS(SELECT * FROM PaymentMethod WHERE UserID=@UserID AND [Type]=@Type AND [status]='deleted'))
BEGIN
Update PaymentMethod
Set [status]='active'
WHERE @UserID=UserID AND [Type]=@Type AND [status]='active'
return 0
END
END
BEGIN TRANSACTION
INSERT INTO [PaymentMethod]
(UserID, [Type], [status])
VALUES (@UserID, @Type, 'active')
DECLARE @NewPaymentMethodID int
SET @NewPaymentMethodID=@@Identity
IF(@Type='Card')
BEGIN
IF(@CardNumber IS NULL OR @Bank IS NULL OR @ExpDate IS NULL)
BEGIN
ROLLBACK TRANSACTION
Raiserror('Using payment method credit card but invalid information is provided',14,3)
return 3
END
---check if card already exists
IF(EXISTS(SELECT * FROM [User] u JOIN PaymentMethod p On u.ID=p.UserID JOIN Has h on p.ID=h.PaymentID WHERE p.Type='Card' AND u.ID=@UserID AND h.CardNum=@CardNumber))
BEGIN
print(@UserID)
RAISERROR('Credit card already exists for this user, ID: ', 14,4)
ROLLBACK TRANSACTION
return 4
END
---create a new creditCard
DECLARE @addCCErr int
EXEC @addCCErr=AddCreditCard @CardNum=@CardNumber, @Bank_1=@Bank, @ExpirationDate=@ExpDate, @PaymentMethodID=@NewPaymentMethodID
IF (@addCCErr <> 0)
BEGIN
RAISERROR('Error: Credit Card cannot be added ',14,5)
ROLLBACK TRANSACTION
return 5
END
END
COMMIT TRANSACTION
Return 0
END
GO
CREATE Procedure [dbo].[createTransaction]
(@TransactionID Int output,
@Location nvarchar(40),
@Price money,
@Datetime Date,
@AddressID int)
AS
BEGIN
If(@Price IS NULL OR @Price<0)
BEGIN
RAISERROR('Price is null or negative', 14,1);
return 1
END
IF(@Location is NULL)
BEGIN
RAISERROR('Location is null', 14,2);
return 2
END
If(@Datetime!=(SELECT CAST( GETDATE() AS Date )))
BEGIN
RAISERROR('Location is null', 14,2);
return 2
END
INSERT INTO [Transaction]
( [Location], Price,[Datetime], AddressID)
VALUES ( @Location,@Price, @DateTime, @AddressID)
DECLARE @newTransactionID int;
SET @newTransactionID = @@IDENTITY;
SET @TransactionID = @newTransactionID;
Return 0
END
GO
CREATE procedure [dbo].[CreateShipment]
@TrackingNumber nvarchar(20) = null, @ShippingMethod nvarchar(20)=null,
@TransactionID int, @ShippingID int OUTPUT
AS
IF @TransactionID is null
begin
RAISERROR ('Error: Transaction ID cannot be null.',14,1)
return 1
end
IF @TransactionID NOT IN (SELECT ID from [Transaction])
begin
RAISERROR ('Error: There is no such transaction.',14,1)
return 2
end
IF (SELECT ShippingID from BUY where TransactionID = @TransactionID) is NOT NULL
begin
RAISERROR ('Error: There is a shipping already.',14,1)
return 2
end
Begin Transaction
Insert into [Shipping](ShipDate,ShippingMethod, TrackingNumber)
values(NULL,@ShippingMethod,@TrackingNumber)
Declare @newID int
Set @newID = @@IDENTITY
Set @ShippingID = @newID
Update Buy
Set ShippingID = @newID
WHERE TransactionID = @TransactionID
-- Check for errors
DECLARE @Status SMALLINT
SET @Status = @@ERROR
IF @Status <> 0
BEGIN
-- Return error code to the calling program to indicate failure.
PRINT 'An error occurred when creating shippment'
ROLLBACK TRANSACTION
RETURN(@Status)
END
ELSE
BEGIN
-- Return 0 to the calling program to indicate success.
PRINT 'Shippment created successfully.'
COMMIT TRANSACTION
RETURN(0)
END
GO
---buy item
CREATE Procedure [dbo].[BuyItem]
(
@ItemID Int,
@BuyerID Int,
@ShippingID Int=NULL,
@PaymentMethodID Int,
@Location_1 nvarchar(40) = NULL,
@Price_1 money,
@AddressID_1 int = NULL,
@BuyID Int output
)
AS
BEGIN
BEGIN TRANSACTION
If( @ItemID is NULL OR Not Exists(SELECT * FROM Items Where ID=@ItemID ))
Begin
RAISERROR('ItemID is null or item does not exist ', 14, 1);
ROLLBACK TRANSACTION
return 1
End
if( (select [status] from Items WHERE ID = @ItemID) <> 'unsold')
BEGIN
RAISERROR('Item is not available ', 14, 1);
ROLLBACK TRANSACTION
return 2
END
if(@BuyerID is NULL OR NOT Exists(SELECT * FROM [USER] Where ID=@BuyerID))
BEGIN
RAISERROR('BuyerID is null or User does not exist', 14, 2);
ROLLBACK TRANSACTION
return 3
END
IF (NOT Exists(SELECT * FROM Buyer Where ID=@BuyerID))
BEGIN
INSERT INTO [Buyer] (ID) VALUES (@BuyerID)
END
IF (@@error <> 0)
BEGIN
ROLLBACK TRANSACTION
return 7
END
if(@PaymentMethodID is NULL OR NOT Exists(SELECT * FROM PaymentMethod Where ID=@PaymentMethodID))
BEGIN
RAISERROR('PaymentMethodID is null or does not exist', 14, 2);
ROLLBACK TRANSACTION
return 4
END
if( NOT Exists(SELECT * FROM PaymentMethod Where ID=@PaymentMethodID AND UserID=@BuyerID) )
BEGIN
RAISERROR('Payment method is not associated with the user', 14, 3);
ROLLBACK TRANSACTION
return 5
END
If(@Price_1 IS NULL OR @Price_1<0)
BEGIN
RAISERROR('Price is null or negative', 14,4);
ROLLBACK TRANSACTION
return 6
END
DECLARE @DateTime_1 Date
SET @DateTime_1 = (SELECT CAST( GETDATE() AS Date ))
DECLARE @NewTransactionID int
DECLARE @ErrorCode int
IF(@Location_1 is NULL)
BEGIN
set @Location_1 = 'Shipping';
END
---create transaction
EXEC @ErrorCode= createTransaction @TransactionID=@NewTransactionID output, @Location=@Location_1, @Price=@Price_1, @DateTime=@DateTime_1, @AddressID=@AddressID_1
IF(@ErrorCode<>0)
BEGIN
ROLLBACK TRANSACTION
return 7
END
IF(@Location_1 = 'Shipping')
BEGIN
---create shipping (if it is needed)
EXEC @ErrorCode= [dbo].[CreateShipment]
@TransactionID = @NewTransactionID, @ShippingID = @ShippingID Output
IF(@ErrorCode<>0)
BEGIN
ROLLBACK TRANSACTION
return 7
END
END
---createBuy
INSERT INTO [Buy]
( TransactionID, ItemID, BuyerID, ShippingID, PaymentMethodID)
VALUES ( @NewTransactionID,@ItemID, @BuyerID, @ShippingID, @PaymentMethodID)
--Change Item status
Update Items
set Status = 'sold'
where ID = @ItemID
IF (@@error <> 0)
BEGIN
ROLLBACK TRANSACTION
return 7
END
DECLARE @newBuyID int;
SET @newBuyID = @@IDENTITY;
SET @NewBuyID = @BuyID;
COMMIT TRANSACTION
Return 0
END
GO
CREATE Procedure [dbo].[Updateshipment]
--This changes shipping information. It does not need Shipping ID. Instead, it needs a transactionID
--and update its shipping.
@TrackingNumber nvarchar(20), @ShippingMethod nvarchar(20)=null,
@TransactionID int
AS
IF @TransactionID is null
begin
RAISERROR ('Error: Transaction ID cannot be null.',14,1)
return 1
end
IF @TransactionID NOT IN (SELECT TransactionID from BUY)
begin
RAISERROR ('Error: There is no such transaction.',14,1)
return 2
end
IF (SELECT ShippingID from BUY where TransactionID = @TransactionID) is NULL
begin
RAISERROR ('Error: There is no shipping to update. Please create one first',14,1)
return 2
end
UPDATE Shipping
SET ShippingMethod = @ShippingMethod,
TrackingNumber = @TrackingNumber,
ShipDate = GETDATE()
WHERE ID = (Select ShippingID FROM Buy
Where TransactionID = @TransactionID)
-- Check for errors
DECLARE @Status SMALLINT
SET @Status = @@ERROR
IF @Status <> 0
BEGIN
-- Return error code to the calling program to indicate failure.
PRINT 'An error occurred when creating shippment'
RETURN(@Status)
END
ELSE
BEGIN
-- Return 0 to the calling program to indicate success.
PRINT 'Shippment created successfully.'
RETURN(0)
END
GO
CREATE Procedure [dbo].[Delete_Shipmment]
--This changes shipping information. It does not need Shipping ID. Instead, it needs a transactionID
--and update its shipping.
@TransactionID int
AS
IF @TransactionID is null
begin
RAISERROR ('Error: Transaction ID cannot be null.',14,1)
return 1
end
IF @TransactionID NOT IN (SELECT TransactionID from BUY)
begin
RAISERROR ('Error: There is no such transaction.',14,1)
return 2
end
IF (SELECT ShippingID from BUY where TransactionID = @TransactionID) is NULL
begin
RAISERROR ('Error: There is no shipping to delete.',14,1)
return 2
end
DECLARE @shippingID int
Select @shippingID = ShippingID FROM Buy
Where TransactionID = @TransactionID
UPDATE Buy
SET ShippingID = NULL
WHERE TransactionID = @TransactionID
DELETE Shipping
WHERE ID = @shippingID
-- Check for errors
DECLARE @Status SMALLINT
SET @Status = @@ERROR
IF @Status <> 0
BEGIN
-- Return error code to the calling program to indicate failure.
PRINT 'An error occurred when creating shippment'
RETURN(@Status)
END
ELSE
BEGIN
-- Return 0 to the calling program to indicate success.
PRINT 'Shippment Deleted successfully.'
RETURN(0)
END
GO
CREATE procedure [dbo].[deleteAddress](@UID int, @AddrID int)
As
IF(@AddrID is NULL OR Not Exists(SELECT ID FROM [Address] where @AddrID = ID))
BEGIN
RAISERROR('Addr cannot be null or does not exist in PaymentMethod', 14, 1);
return 1
END
IF(@UID is NULL OR Not Exists(SELECT UserID FROM [Address] where @UID = UserID))
BEGIN
RAISERROR('UID cannot be null or does not exist in Address', 14, 1);
return 2
END
If(Not Exists(
SELECT * FROM [Address]
WHERE ID= @AddrID AND UserID=@UID
))Begin
RAISERROR('There is no user who own this Address.', 14, 3);
return 3
End
Update [Address]
Set [status] = 'deleted'
WHERE (ID = @AddrID)
return 0
GO
CREATE Procedure [dbo].[DeleteItem](
@ItemID Int,
@SellerID Int
)
AS
BEGIN
IF(@ItemID is NULL OR Not Exists(SELECT * FROM Items Where ID=@ItemID ))
BEGIN
RAISERROR('ItemID cannot be null or does not exist', 14, 1);
return 1
END
If( @SellerID is NULL OR Not Exists(SELECT * FROM Seller Where ID=@SellerID ))
Begin
RAISERROR('Seller ID is null or does not exist ', 14, 2);
return 2
End
If((select [Status] from Items where @ItemID=ID) ='sold')
Begin
RAISERROR('Item has been sold ', 14, 2);
return 2
end
If(Not Exists(
SELECT * FROM Items
WHERE ID=@ItemID AND SellerID=@SellerID
))Begin
RAISERROR('There is no seller selling this product or no product is sold by this seller.', 14, 3);
return 3
End
DELETE Items
WHERE ( ID = @ItemID AND SellerID= @SellerID and [Status] ='unsold' )
return 0
END
GO
CREATE procedure [dbo].[deletePaymentMethod](@UID int, @paymentID int)
As
BEGIN
IF(@paymentID is NULL OR Not Exists(SELECT ID FROM PaymentMethod where @paymentID = ID))
BEGIN
RAISERROR('paymentMethodID cannot be null or does not exist in PaymentMethod', 14, 1);
return 1
END
IF(@UID is NULL OR Not Exists(SELECT UserID FROM PaymentMethod where @UID = UserID))
BEGIN
RAISERROR('UID cannot be null or does not exist in PaymentMethod', 14, 1);
return 2
END
If(Not Exists(
SELECT * FROM PaymentMethod
WHERE ID= @paymentID AND UserID=@UID
))Begin
RAISERROR('There is no user who own this paymentID.', 14, 3);
return 3
End
IF(NOT EXISTS(SELECT * FROM Buy Where PaymentMethodID= @paymentID))
BEGIN
DELETE FROM PaymentMethod
WHERE ID=@paymentID
DECLARE @PaymentType NVarchar(60)
SELECT @PaymentType=[Type] FROM PaymentMethod WHERE @paymentID=ID
IF(@PaymentType='Card')
BEGIN
DECLARE @CardNum NVarchar(32)
SELECT @CardNum=CardNum FROM Has Where PaymentID=@paymentID
DELETE FROM CreditCard WHERE @CardNum=CardNum
END
END
ELSE IF(EXISTS(SELECT * FROM Buy Where PaymentMethodID= @paymentID))
BEGIN
Update PaymentMethod
Set [status] = 'deleted'
WHERE (ID = @paymentID)
return 0
END
END
GO
CREATE procedure [dbo].[Login]
@Username nvarchar(20),
@Password nvarchar(20)
As
IF @Username is null
begin
RAISERROR ('Error: Name cannot be null.',14,1)
return 1
end
IF(NOT EXISTS(SELECT * FROM [User] Where username=@Username))
BEGIN
RAISERROR ('Error: User does not exist',14,2)
return 2
END
If @Password is null
begin
RAISERROR ('Error: Password cannot be null.',14,3)
return 3
end
GO
CREATE Procedure [dbo].[ModifyItem](
@ItemID Int,
@SellerID Int,
@Name Nvarchar(40)=null,
@Keyword Nvarchar(40)=null,
@Type Nvarchar(40)=null,
@Description Nvarchar(400)=null,
@Price Money=null
)
AS
BEGIN
IF(@ItemID is NULL OR Not Exists(SELECT * FROM Items Where ID=@ItemID ))
BEGIN
RAISERROR('ItemID cannot be null or does not exist', 14, 1);
return 1
END
If( @SellerID is NULL OR Not Exists(SELECT * FROM Seller Where ID=@SellerID ))
Begin
RAISERROR('Seller ID is null or does not exist ', 14, 2);
return 2