Order transaction-Stored Procedure

Source: Internet
Author: User
Create proc createorder
@ Orderid nvarchar (50), -- Order Number
@ Userid int, -- User ID
@ Address nvarchar (255), -- recipient address
@ Totalmoney money output -- total amount
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
Rollback transaction
Commit transaction
-- Rollback transaction
-- Commit transaction
-- Primary key (ID) Identity (1, 1)

Order transaction-Stored Procedure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.