![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
CREATE
TABLE
[
dbo
]
.ShoppingCart
2 (
3 CartID INT IDENTITY ( 1 , 1 ) NOT NULL PRIMARY KEY ,
4 UserID INT NOT NULL ,
5 ProductID INT NOT NULL ,
6 Quantity INT NOT NULL
7 )
8 GO
9
10 CREATE PROC AddToShoppingCart
11 (
12 @UserID INT ,
13 @ProductID INT
14 )
15 AS
16 DECLARE @ProductName VARCHAR ( 100 ), @UnitPrice MONEY
17 IF EXISTS ( SELECT * FROM ShoppingCart WHERE ProductID = @ProductID AND UserID = @UserID )
18 BEGIN
19 UPDATE
20 ShoppingCart
21 SET
22 Quantity = Quantity + 1
23 WHERE
24 ProductID = @ProductID
25 AND
26 UserID = @UserID
27 END
28 ELSE
29 BEGIN
30 SELECT
31 @ProductName = ProductName, @UnitPrice = UnitPrice
32 FROM
33 Product
34 WHERE
35 ProductID = @ProductID
36
37 INSERT INTO ShoppingCart(UserID,ProductID,Quantity) VALUES ( @UserID , @ProductID , 1 )
38 END
39 GO
2 (
3 CartID INT IDENTITY ( 1 , 1 ) NOT NULL PRIMARY KEY ,
4 UserID INT NOT NULL ,
5 ProductID INT NOT NULL ,
6 Quantity INT NOT NULL
7 )
8 GO
9
10 CREATE PROC AddToShoppingCart
11 (
12 @UserID INT ,
13 @ProductID INT
14 )
15 AS
16 DECLARE @ProductName VARCHAR ( 100 ), @UnitPrice MONEY
17 IF EXISTS ( SELECT * FROM ShoppingCart WHERE ProductID = @ProductID AND UserID = @UserID )
18 BEGIN
19 UPDATE
20 ShoppingCart
21 SET
22 Quantity = Quantity + 1
23 WHERE
24 ProductID = @ProductID
25 AND
26 UserID = @UserID
27 END
28 ELSE
29 BEGIN
30 SELECT
31 @ProductName = ProductName, @UnitPrice = UnitPrice
32 FROM
33 Product
34 WHERE
35 ProductID = @ProductID
36
37 INSERT INTO ShoppingCart(UserID,ProductID,Quantity) VALUES ( @UserID , @ProductID , 1 )
38 END
39 GO