34,588
社区成员
发帖
与我相关
我的任务
分享
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 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