34,588
社区成员
发帖
与我相关
我的任务
分享
create table tb_test(lszh varchar(20),before varchar(2) default 'n',after varchar(2) default 'n')
create proc proc_test1
as
begin
insert into tb_test(lszh) select lszh from tb_lszh --假设一次数插入20条数据
declare @lszh varchar(20)
while exists(select lszh from tb_test where after='n')
begin
select top(1) @lszh=lszh from tb_test where after='n'
update tb_test set before='y'
exec proc_test_2 --执行另一个存储过程
update tb_test set after='y'
end
end
--现遇到个奇葩问题,我前台程序中调用存储过程的时候exec proc_test1 的时候成功插入20条数据在tb_test表中,流水号什么的都是对的
--但是存储过程有时候执行会出现类似下面数据的结构
lszh before after
1 y y
2 y y
3 y y
4 y y
5 y y
6 y n --这里是一个y,一个n
7 n n
8 n n
9 n n
10…………
11…………后面的都是n,n了不写了
就在这个时候,我悄悄在后台数据库中执行了如下语句,exec proc_test1,这些完了,剩下的数据毫无错误的被执行完了
鬼火一怒,我加了个捕获异常的在exec proc_test_2 上面
create table tb_error(lszh varchar(20),err_message varchar(200),err_line int)
所以存储过程就变成这样了
create proc proc_test1
as
begin
insert into tb_test(lszh) select lszh from tb_lszh --假设一次数插入20条数据
declare @lszh varchar(20)
while exists(select lszh from tb_test where after='n')
begin
select top(1) @lszh=lszh from tb_test where after='n'
update tb_test set before='y'
begin try
exec proc_test_2 --执行另一个存储过程
end try
begin catch
insert into tb_error(lszh,err_message,err_line) select @lszh,error_message(),error_line()
end catch
update tb_test set after='y'
end
end
--但是这似乎没有什么卵用,表tb_error中捕获不到任何错误,存储过程执行的时候还是会遇到上面那个奇葩的问题(ps,貌似也不是每次执行都会这样,就观察而已有的时候数据量大的时候会出现这样,但是绝对不是超时,因为我跟踪过执行时间,很短)
就在这个时候,我又悄悄在后台数据库中执行了如下语句,exec proc_test1,这些完了,剩下的数据毫无错误的被执行完了,没报任何错误
于是鬼火一怒,我又改了下存储过程
create proc proc_test1
as
begin
begin try
insert into tb_test(lszh) select lszh from tb_lszh --假设一次数插入20条数据
declare @lszh varchar(20)
while exists(select lszh from tb_test where after='n')
begin
select top(1) @lszh=lszh from tb_test where after='n'
update tb_test set before='y'
exec proc_test_2 --执行另一个存储过程
update tb_test set after='y'
end
end try
begin catch
insert into tb_error(lszh,err_message,err_line) select @lszh,error_message(),error_line()
end catch
end
--但是,还是铺货不到任何错误,执行是还是会发生上面的奇葩现象
---于是,我又鬼火了,在 proc_test_2里面加
alter proc proc_test_2
as
begin
begin try
…………
end try
begin catch
insert into tb_error(lszh,err_message,err_line) select @lszh,error_message(),error_line()
end catch
end
--然而,还是没有捕获到异常,程序大哥心情好了的时候还是给你来一个这样的奇葩
lszh before after
1 y y
2 y y
3 y y
4 y y
5 y y
6 y n --这里是一个y,一个n
7 n n
8 n n
9 n n
10…………
11…………后面的都是n,n了不写了
----各位英雄好汉,来拯救下我吧,谢谢