日期:2014-05-17  浏览次数:20795 次

怎么判断存储过程是否执行
就行sql语句可以判断@@ROWCOUNT来判断执行成功与否
那存储过程怎么判断呢
存储过程 sql 是否执行成功

------解决方案--------------------
这个要看你的sp是来干嘛的,如果是返回数据,就检查里面有没有数据,如果是一些业务操作比如修改数据,那就检查是否有操作的痕迹,目前没见过能直接判断出来的,等楼下补充
------解决方案--------------------
数据变了,就执行了啊
------解决方案--------------------
用数据库跟踪服务sql srver Profiler查看数据库有没有变动
------解决方案--------------------

create table #tab(ft int,ftn int,ftname varchar(20),old int)
insert into #tab
select 0,1,'a',1 union all
select 0,2,'b',2 union all
select 0,3,'c',3 union all
select 1,8,'d',1 union all
select 1,9,'e',1 union all
select 2,10,'f',2 union all
select 2,11,'g',2 union all
select 3,12,'h',3


alter proc usp_#tab
as
declare @a varchar(50)
begin
if 1=1
begin
delete from #tab
set @a='执行成功'
end
if 1=2
begin
set @a='执行失败'
end
select @a
end

------解决方案--------------------
如果你的系统是2005以后的话,可以用try catch:


create proc proc_t(@i int)
as

select 5 * 1.0 / @i
go


--1.不会报错
begin try
begin tran
exec proc_t 1
select '执行成功'
commit tran
end try
begin catch
  select '执行失败'
  if @@trancount >0 
     rollback
end catch


--2.这次会报错,因为分母为0
begin try
begin tran
exec proc_t 0
select '执行成功'
commit tran
end try
begin catch
  select '执行失败'
  if @@trancount >0 
     rollback
end catch


------解决方案--------------------

create proc proc_t(@i int)
as

select 5 * 1.0 / @i
go


--1.不会报错
begin try
begin tran
exec proc_t 1
select '执行成功'
commit tran
end try
begin catch
  select '执行失败'
  if @@trancount >0 
     rollback
end catch


--2.这次会报错,因为分母为0
begin try
begin tran
exec proc_t 0
select '执行成功'
commit tran
end try
begin catch
  select '执行失败'
  if @@trancount >0 
     rollback
end catch



------解决方案--------------------
重要的业务可以
考虑建立一张日志表
来记录
------解决方案--------------------
把存储过程拆开来,传几个参数进去,看看你执行的数据是否正确,正确的话就是 对的