begin tran
insert into table1
select ....
insert into table2
select ....
commit tran
当被包含在事务中任何一个表插入失败时,事务中的任何操作都将回滚。
insert into table1
select ....
insert into table2
select ....
commit tran
当被包含在事务中任何一个表插入失败时,事务中的任何操作都将回滚。
解决方案 »
- 请教事务操作存储过程的问题
- 初学者提个问题
- 第一次select会导致无法恢复的超时,试第2次就好了,这是什么原因
- 如何从一个句子中找出某一个单词
- sql2000差异备份问题
- SQL2005更新遇到冲突问题
- 通过SQLCLR获取SQLServer服务器上控制面板设定的日期格式不正确?
- 高手帮忙! 我把SQL SERVER的一个表里的数据删除了如何恢复???
- 这个问题恐怕高手也要头痛!如果是高手,不妨学习一下,呵呵
- 散些分,没多少,呵呵
- *********** ??? 求教一挑战性排序方法 ???? *******************************************************
- 这个存储过程如何写?
不行insert into table1
失败了
insert into table2
还能执行
declare @tran_point int --控制事务嵌套
set @tran_point=@@trancount --保存事务点
if @tran_point=0
begin tran tran_[过程名称]/*执行事务体*/
--常用跳出语句
if @@ERROR <> 0
begin
set @return= [过程内异常编号]
goto err_lab --跳出
end if @@rowcount=0
begin
set @return= [过程内异常编号]
goto err_lab --跳出
end
--结束事务处理
if @tran_point=0
commit tran tran_[过程名称]
goto return_lab
err_lab:
if @return<100 set @return=@errcode +@return
if @tran_point=0
rollback tran tran_[过程名称]
return_lab:
return @return
insert into #aa
select 'aabbs',10
insert into #bb
select 'asdf',1
commit tran
go
begin tran
insert into #aa
select 'aabbs',10
insert into #bb
select 'asdf','a'
commit tran
go
select * from #aa
select * from #bb
--第一个只是字符截位,没有语法错误所以通过,第二种在转换只有语法错误,所以会回滚。
--zz5290()的通过程序判断的方法不错!
if @tran_point=0
commit tran tran_[过程名称]
@tran_point只在开始的时候set @tran_point=@@trancount
后面就没有了
所以永远=0?