EXECUTE 后的事务计数指出缺少了 COMMIT 或 ROLLBACK TRANSACTION 语句。原计数 = 0,当前计数 = 1。

laoye's 2009-04-13 01:41:50



ALTER PROC pro_ShoppingByEMoney
(
@OrderID char(18),
@UserName varchar(20),
@FullDate datetime,
@FullIP varchar(20),
@OrderMoney money,
@ret int out
)
AS

declare @GoodsID char(12)
declare @CardNo char(12)
declare @SourceType int
declare @AEMoneyBuy decimal(19,4)
declare @AEMoneyPar decimal(19,4)
declare @AEMoneyBuyM decimal(19,4)
declare @AEMoneyParM decimal(19,4)
declare @BEMoneyBuy decimal(19,4)
declare @BEMoneyPar decimal(19,4)
declare @AddFlag int

select @GoodsID=''
select @CardNo=''
select @SourceType=3 -- 购物消费E元
select @BEMoneyBuy=0
select @BEMoneyPar=0
select @AddFlag=1 -- E元消减
select @ret=0 -- 不能使用E元支付

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

BEGIN TRANSACTION
--select * from memberEMoney where username='estore'
--commit transaction
-- check user EMoney and set AEMoneyBuy AEMoneyPar

-- 确定订单有效 start add by Lisa on 2005-02-03
declare @OrderPayCount int
declare @OPayMoney money
set @OPayMoney = 0
set @OrderPayCount = 1
select @OrderPayCount=count(OrderBankid), @OPayMoney=isnull(sum(ordermoney),0) from OrderMst where OrderbankID=@OrderID and (OrderStatus=0 or OrderStatus=1)
-- 防止重复支付 start
declare @OrderCount int
set @OrderCount = 0
select @OrderCount=count(orderid) from fullrecord where OrderID=@OrderID
-- 防止重复支付
if (@OrderCount=0 and @OrderPayCount>0 and @OPayMoney=@OrderMoney)
begin
select @AEMoneyBuyM=AEMoneyBuy, @AEMoneyParM=AEMoneyPar from memberEMoney where username=@UserName
-- 如果购买的A类E元大于等于订单金额,则消减购买的A类E元
if @AEMoneyBuyM>=@OrderMoney
begin
set @AEMoneyBuy = @OrderMoney
set @AEMoneyPar = 0
set @AEMoneyBuyM = @AEMoneyBuyM - @OrderMoney
select @ret=1 -- 使用购买的A类E元支付
end
else
begin
-- 如果购买的A类E元大小于订单金额,但是A类E元总数大于等于订单金额,则优先消减购买的A类E元,其余部分消减赠送的A类E元
if (@AEMoneyBuyM+@AEMoneyParM)>=@OrderMoney
begin
set @AEMoneyBuy = @AEMoneyBuyM
set @AEMoneyPar = @OrderMoney-@AEMoneyBuyM
set @AEMoneyParM = @AEMoneyBuyM+@AEMoneyParM-@OrderMoney
set @AEMoneyBuyM = 0
select @ret=2 -- 使用A类E元支付
end
else
begin
select @ret=0 -- 不能使用E元支付
end
end

-- if enoght
if @ret>0
begin
-- fullRecord
-- 如果没有下面exec则执行正常
exec usp_FullRecordInsert @GoodsID, @OrderID, @CardNo, @UserName, @FullDate, @FullIP, @SourceType, @AEMoneyBuy, @AEMoneyPar, @BEMoneyBuy, @BEMoneyPar, @AddFlag
---

-- update user EMoney
update memberEMoney set AEMoneyBuy=@AEMoneyBuyM, AEMoneyPar=@AEMoneyParM, lastdate=getdate() where username=@UserName

if @@error>0
begin
rollback transaction
set @ret=-1
end
else
begin
commit transaction
end
end

--commit transaction

end

--commit transaction
--SET TRANSACTION ISOLATION LEVEL READ COMMITTED
--print @ret



return @ret
-- else return





存储过程第一次执行正常,但第二次执行的时候就出现EXECUTE 后的事务计数指出缺少了 COMMIT 或 ROLLBACK TRANSACTION 语句。原计数 = 0,当前计数 = 1。

如果我注释存储过程里面的exec usp_FullRecordInsert 则每次都可正常执行, 请问是什么原因呢? 我该如何修改. 谢谢

...全文
492 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fakuan 2009-09-30
  • 打赏
  • 举报
回复
sfsadfs
nzperfect 2009-04-13
  • 打赏
  • 举报
回复
如果用@@error来捕获错误,那么每个update insert delete exec随后都要判断@@error.
     exec usp_FullRecordInsert  @GoodsID, @OrderID, @CardNo, @UserName, @FullDate, @FullIP, @SourceType, @AEMoneyBuy, @AEMoneyPar, @BEMoneyBuy, @BEMoneyPar, @AddFlag
--这里也需要判断@@error-------------------
if @@error>0
begin
rollback transaction
set @ret=-1
end
claro 2009-04-13
  • 打赏
  • 举报
回复
帮顶。
  • 打赏
  • 举报
回复
3楼只是写了你代码结构的问题,你本身的问题没有写。你本帖的问题和你的那么存储过程有关系的。这要仔细查看你的存储过程的代码才能找出问题
Yang_ 2009-04-13
  • 打赏
  • 举报
回复
贴出usp_FullRecordInsert 的代码,应该那里面有事务操作

另外,这段逻辑有问题

  if @ret>0           
begin
-- fullRecord
-- 如果没有下面exec则执行正常
exec usp_FullRecordInsert @GoodsID, @OrderID, @CardNo, @UserName, @FullDate, @FullIP, @SourceType, @AEMoneyBuy, @AEMoneyPar, @BEMoneyBuy, @BEMoneyPar, @AddFlag
---

-- update user EMoney
update memberEMoney set AEMoneyBuy=@AEMoneyBuyM, AEMoneyPar=@AEMoneyParM, lastdate=getdate() where username=@UserName

if @@error>0
begin
rollback transaction
set @ret=-1
end
else
begin
commit transaction
end
end
--应该加这一句
else
begin
rollback transaction
end
  • 打赏
  • 举报
回复
看了一下你的结构,有一点缺陷。

if @ret>0
begin
-- fullRecord
-- 如果没有下面exec则执行正常
exec usp_FullRecordInsert @GoodsID, @OrderID, @CardNo, @UserName, @FullDate, @FullIP, @SourceType, @AEMoneyBuy, @AEMoneyPar, @BEMoneyBuy, @BEMoneyPar, @AddFlag [/color]
---

-- update user EMoney
update memberEMoney set AEMoneyBuy=@AEMoneyBuyM, AEMoneyPar=@AEMoneyParM, lastdate=getdate() where ame=@UserName

if @@error>0
begin
rollback transaction
set @ret=-1
end
else
begin
commit transaction
end
end

--commit transaction

end

也就是@ret>0的时候,会执行下面的对事务的提交或者回滚。但是当@ret不大于0的时候呢?下面的是不是就不执行了?按照你前面的语句@ret是有可能=0的,那么自然就缺少了commit或者rollback
ws_hgo 2009-04-13
  • 打赏
  • 举报
回复
先看下
csdyyr 2009-04-13
  • 打赏
  • 举报
回复
usp_FullRecordInsert的代码?
Zoezs 2009-04-13
  • 打赏
  • 举报
回复
大佬们都解释清楚了。我学习学习。
  • 打赏
  • 举报
回复
还是详细调试下,可能是使用这个存储过程向FullRecord表中插入数据的时候,发生了错误,导致的。

还有你的 @GoodsID,@CardNo这些变量初始化后,貌似再没有进行处理吧
laoye's 2009-04-13
  • 打赏
  • 举报
回复
附件上:usp_FullRecordInsert内容,这里是一个插入语句.

  CREATE PROC usp_FullRecordInsert  
(

@GoodsID varchar(12),
@OrderID varchar(18),
@CardNo char(12),
@UserName varchar(20),
@FullDate datetime,
@FullIP varchar(20),
@SourceType int,
@AEMoneyBuy decimal(19,4),
@AEMoneyPar decimal(19,4),
@BEMoneyBuy decimal(19,4),
@BEMoneyPar decimal(19,4),
@AddFlag int
)
AS
INSERT INTO [FullRecord]
(
[GoodsID],
[OrderID]
-- ......
)
VALUES
(
@GoodsID,
@OrderID
--........
)


22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧