一个存储过程的问题,在执行时会不会跳语句!

ntfs2 2004-03-30 12:21:37
CREATE proc dbo.proc_di_sall --外挂和实物卡销售
@ka_id int, --卡类型的id
@user_id int
as
begin
declare @sall_money money --销售金额
declare @have_money money --当前有多少钱
declare @sall_ka_id int --有销售卡的id
set nocount on
begin transaction
if exists(select 1 from sw_ka_detail where fid=@ka_id and status=0) begin --有卡销售
select @sall_money=(select xsjg from sw_ka_type where id=@ka_id) --取得销售金额
select @have_money=(select money from tuser where fid=@user_id) --取得当前用户的钱数
if @have_money<=@sall_money begin -- 钱不够
select 0 as a1,'钱不够' as a2,'钱不够' as a3
return
end else begin -- 钱够了,开始销售
select @sall_ka_id=(select top 1 id from sw_ka_detail where fid=@ka_id and status=0) --取一条销售的id号码
update tuser set money=money-@sall_money where fid=@user_id --减钱
if(@@error<>0) goto error
update sw_ka_detail set status=1 where id=@sall_ka_id --修改卡的状态
if(@@error<>0) goto error
insert into sw_ka_sall (ka_id,sall_time,sall_money,ka_ren) values(@sall_ka_id,getdate(),@sall_money,@user_id) --添加销售纪录
if(@@error<>0) goto error
select id as a1,ka_name as a2,ka_pass as a3 from sw_ka_detail where id=@sall_ka_id --显示用户名和密码
end
end else begin --无卡销售
select 1 as a1,'无卡销售' as a2 ,'无卡销售' as a3
end
commit transaction
set nocount off
return
end
error:
rollback transaction
raiserror 30001 '出错'


===========================================

高手看看,这个存储过程有没有问题,系统会不会在执行时跳过部分语句的执行!
...全文
40 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ntfs2 2004-04-01
  • 打赏
  • 举报
回复
我去试一下!谢谢!
3天后结帖!
ntfs2 2004-03-30
  • 打赏
  • 举报
回复
兄弟们,执行时没什么问题,就是有时会不执行
===============================================
if(@@error<>0) goto error
update sw_ka_detail set status=1 where id=@sall_ka_id --修改卡的状态
if(@@error<>0) goto error
insert into sw_ka_sall (ka_id,sall_time,sall_money,ka_ren) values(@sall_ka_id,getdate(),@sall_money,@user_id) --添加销售纪录
if(@@error<>0) goto error


===========================================
直接执行
=======================
select id as a1,ka_name as a2,ka_pass as a3 from sw_ka_detail where id=@sall_ka_id --显示用户名和密码

===============================
我买的是网络上的数据库,会不会是网络有问题呢?

Rotaxe 2004-03-30
  • 打赏
  • 举报
回复
没看出问题,有什么错误吗?
vileboy 2004-03-30
  • 打赏
  • 举报
回复

看不怎么明白,应当会跳过吧
progress99 2004-03-30
  • 打赏
  • 举报
回复
當錯誤的嚴重級別很高時,if(@@error<>0) goto error 無用,程式會自己中止,
這樣處理:

CREATE proc dbo.proc_di_sall --外挂和实物卡销售
@ka_id int, --卡类型的id
@user_id int
as
begin
declare @sall_money money --销售金额
declare @have_money money --当前有多少钱
declare @sall_ka_id int --有销售卡的id
set nocount on
SET XACT_ABORT ON
...


当 SET XACT_ABORT 为 ON 时,如果 Transact-SQL 语句产生运行时错误,整个事务将终止并回滚。
ntfs2 2004-03-30
  • 打赏
  • 举报
回复
top

22,209

社区成员

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

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