/*alter 修改*/create proc InsertOrder @OrderCode nvarchar(50), @CusId int as begin begin tran--开启事务 begin try --计算总金额 declare @totalPri decimal(8,2) select @totalPri=sum(price*[count]) from carinfo where cusid=@CusId --向主表插入数据 insert into ordermaster(ocode,odate,oprice,cusid) values(@OrderCode,GETDATE(),@totalPri,@CusId) --向从表插入数据 insert into orderslave(bid,bcount,ordercode) select bookid,[count],@OrderCode from carinfo where cusid=@CusId --删除购物车中的数据 delete from carInfo where CusId=@CusId commit tran--提交事务 end try begin catch rollback tran--一旦发生异常,回滚事务 end catch end
创建存储过程,并用事务提交订单
时间: 2024-10-08 11:00:54