关于存储过程的返回值,郁闷啊!

shaxiren 2003-12-11 03:32:07
我写了一个存储过程,里面有很多更新语句,
当然是放在一个事务里的,我每更新一张表,都要判断
@@error,而后返回一个自定义的数值,以标明在更新哪张表
时发生错误,遗憾的是即使在更新某一张表时发生错误,错误代号
也是由系统给出的,如-6,而不是我自定义的1,为什么得不到我定义的
返回值呢?
...全文
24 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
shaxiren 2003-12-11
  • 打赏
  • 举报
回复
我delcare @iret
exec @iret = proc_name
这是就应该能得到啊,可是得到的确实系统的,不知何故?
另外我不想象楼上的兄弟一样勇output参数!
zjcxc 元老 2003-12-11
  • 打赏
  • 举报
回复
--下面是例子:
create proc p_process
@re int out --返回处理的结果,0为正确,其他为出错的编号
as
begin tran
处理语句1
if @@error<>0
begin
set @re=1
goto lb_re
end

处理语句2
if @@error<>0
begin
set @re=2
goto lb_re
end
处理语句3
if @@error<>0
begin
set @re=3
goto lb_re
end
commit tran
return

lb_re:
rollback tran
go

--调用:
declare @re int
exec p_process,@re out
select 结果=@re
shaxiren 2003-12-11
  • 打赏
  • 举报
回复
insert into tp_tradedata_error_01 --tac错误(灰记录)公交消费数据入中间表
select id,rectype,tradetype,asn,posno,samno,postradeno,
tradedate,tradetime,premoney,trademoney,
smoney,psamverno,cardtradeno,tac,cityno,
callingno,corpno,subcityno,departno,mac,
collectcardno,instime,tacstate,macstate,
batchno,'1','0'
from ti_tradedata_01
where (tacstate = '1' or macstate = '1') and rectype = '1' --1表示灰记录
and cityno = '2150'

if @@error <> 0 --TAC错误(灰记录)消费数据入中间表失败
begin
rollback
return 3
end

---------------------------------------------------------------------------------------------

insert into tp_tradedata_error_01 --公交消费测试数据入中间表
select id,rectype,tradetype,asn,posno,samno,postradeno,
tradedate,tradetime,premoney,trademoney,
smoney,psamverno,cardtradeno,tac,cityno,
callingno,corpno,subcityno,departno,mac,
collectcardno,instime,tacstate,macstate,
batchno,'5','0'
from ti_tradedata_01 a
where tacstate = '0' and macstate = '0' and cityno = '2150' and
not exists (select 1 from tf_r_icuser b where b.asn = a.asn)


if @@error <> 0 --公交消费测试数据入中间表失败
begin
rollback
return 4

身略了很多
zjcxc 元老 2003-12-11
  • 打赏
  • 举报
回复
你的存储过程代码是怎样写的?

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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