订单存储过程
create proc create_OrderConfrim
@orderId nvarchar(50),--订单号
@userId int,---用户编号
@address nvarchar(255),--收货人地址
@totalMoney money output--总价
as
declare @error int
set @error=0--记录错误
--计算订单的总价(当前用户)
select @totalMoney=sum([count]*UnitPrice) from Cart inner join Books
on Cart.BookId=Books.Id
where Cart.UserId=@userId --向订单主表中插入记录.
begin transaction
insert into dbo.Orders(OrderId, OrderDate, UserId, TotalPrice, PostAddress, state) values(@orderId,getdate(),@userId,@totalMoney,@address,0) set @error=@error+@@error --向订单明细表中插入
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 from Cart where UserId=@userId
set @error=@error+@@error
if @error>0--出现错误了。
begin
rollback transaction
end
else
begin
commit transaction
end