求一存储过程,急!!!!!!!!
create proc test
(
@name varchar(50)
)
as
declare @ret int
declare @err1 int
declare @err2 int
begin tran
insert table1( fsname ) values ( @name )
select @ret = @@identity
select @err1 = @@error
insert table3 ( fiid, fsname ) values ( @ret, @name )
select @err2 = @@error
if( @err1 <> 0 and @err2 <> 0 )
rollback tran
else
commit tran
为了验证事务的正确性,我故意将table2写成了table3,但是执行的时候就报如下的错误,麻烦大家帮忙看看,谢了!
EXECUTE 后的事务计数指出缺少了 COMMIT 或 ROLLBACK TRANSACTION 语句。原计数 = 0,当前计数 = 1。
------解决方案--------------------create proc test
(
@name varchar(50)
)
as
declare @ret int
declare @err1 int
declare @err2 int
declare @trancount int
set @trancount = @@trancount
if @trancount = 0
begin tran
else
save tran proc_test
insert table1( fsname ) values ( @name )
select @ret = @@identity,
@err1 = @@error
insert table3 ( fiid, fsname ) values ( @ret, @name )
select @err2 = @@error
if( @err1 <> 0 or @err2 <> 0 )
begin
if @trancount = 0
rollback tran
else
rollback tran proc_test
end
else
if @trancount = 0
commit tran