Create proc createorder
@ Orderid nvarchar (50), -- Order Number
@ Userid int, -- User ID
@ Address nvarchar (255), -- recipient address
@ Totalmoney money output -- total amount
As
Begin
Declare @ error int
Set @ error = 0
Begin transaction
-- Calculates the total price.
Select @ totalmoney = sum ([count] * unitprice) from cart
Inner join books on cart. bookid = books. ID
Where [email protected]
Set @ [email protected] @ error + @ Error
-- Insert data into the main table of the order
Insert into orders (orderid, orderdate, userid, totalprice, postaddress, [State])
Values (@ orderid, getdate (), @ userid, @ totalmoney, @ address, 0)
Set @ [email protected] @ error + @ Error
-- Insert data into the Order List
Insert into orderbook (orderid, bookid, quantity, unitprice)
Select @ orderid, bookid, [count], unitprice from cart inner join books on cart. bookid = books. ID
Where [email protected]
Set @ [email protected] @ error + @ Error
-- Delete data in the shopping cart table
Delete from cart where [email protected]
Set @ [email protected] @ error + @ Error
-- Judge errors and execute transactions
If @ error> 0
Begin
Rollback transaction
End
Else
Begin
Commit transaction
End
End
-- Rollback transaction
-- Commit transaction
-- Primary key (ID) Identity (1, 1)
Order transaction-Stored Procedure