欢迎投稿

今日深度:

订单事务-存储过程,订单事务存储过程

订单事务-存储过程,订单事务存储过程


create proc Createorder
@orderId nvarchar(50),--订单号
@userId int,--用户编号
@address nvarchar(255),--收货人地址
@totalMoney money output --总金额
as 
begin
declare @error int
set @error=0
begin transaction
--计算总价
select @totalMoney=SUM([count]*Unitprice)from Cart 
inner join Books on Cart.BookId=Books.Id
where UserId=@userId
set @error=@@ERROR+@error
--向订单主表中插入数据
insert into Orders(OrderId,OrderDate,UserId,TotalPrice,PostAddress,[state])
values(@orderId,GETDATE(),@userId,@totalMoney,@address,0)
set @error=@@ERROR+@error
--向订单明细表中插入数据
insert into 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
end
--rollback transaction
--commit transaction
--primary key(Id) identity(1,1)

怎写一个带事务提交的存储过程?

Createp proccreate proc AA1
as
Begin
BEGIN TRANSACTION
--存储过程内容
(
If @@error<>0
If @@TranCount=1
Rollback Transaction
)--中间出现事务每次要进行以此判断
else
If @@TranCount=1
Commit TRANSACTION
End
 

对于存储过程与事务

不对,是两个概念,存储过程是完成一定功能的可重复调用的程序。
即,存储过程是程序。
事务是可以整个撤消的一段操作,可能是一个或几个或部份的存储过程,也可能是一条或几条指令,事务是记录的一系列的操作和变化。
 

www.htsjk.Com true http://www.htsjk.com/shujukunews/2822.html NewsArticle 订单事务-存储过程,订单事务存储过程 create proc Createorder @orderId nvarchar(50),--订单号 @userId int,--用户编号 @address nvarchar(255),--收货人地址 @totalMoney money output --总金额 as begin declare @error...
评论暂时关闭