Order storage process

Source: Internet
Author: User

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

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.