Create proc create_orderconfrim
@ Orderid nvarchar (50), -- Order Number
@ Userid int, --- user ID
@ Address nvarchar (255), -- recipient address
@ Totalmoney money output -- total price
As
Declare @ error int
Set @ error = 0 -- Record Error
-- Calculate the total order price (current user)
Select @ totalmoney = sum ([count] * unitprice) from cart inner join books
On cart. bookid = books. ID
Where cart. userid = @ userid -- insert records into the main table of the order.
Begin transaction
Insert into DBO. orders (orderid, orderdate, userid, totalprice, postaddress, state) values (@ orderid, getdate (), @ userid, @ totalmoney, @ address, 0) set @ error = @ error + @ error -- insert to order list
Insert into DBO. orderbook (orderid, bookid, quantity, unitprice) Select @ orderid, bookid, [count], unitprice from cart inner join books on cart. bookid = books. ID where cart. userid = @ userid
Set @ error = @ error + @ error -- delete the item in the shopping cart
Delete from cart where userid = @ userid
Set @ error = @ error + @ Error
If @ error> 0 -- an error occurs.
Begin
Rollback transaction
End
Else
Begin
Commit transaction
end