📄 50800.htm
字号:
<link href="./dzs_cs.css" rel="stylesheet" type="text/css" /><table width="96%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td> </td> </tr> <tr> <td height="24" align="center" valign="bottom" class="d_font3">SQL Server怪辟:异常与孤立事务</td> </tr> <tr> <td height="3" bgcolor="#E3E3E3"></td> </tr> <tr> <td> </td> </tr> <tr> <td class="d_font4"><P><STRONG>一、首先从SQL Server中的Error讲起,SQL中错误处理有些怪辟,错误级别同是16,但结果都不同。</STRONG></P>
<P>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1>
<TBODY>
<TR>
<TD class=code bgColor=#e6e6e6><PRE><P>select * from 一个不在的表<BR> if @@error<>0<BR> print '这个没有输出'<BR> go <BR> raiserror('',16,3)<BR> if @@error<>0<BR> print '这个输出了'<BR> go</P><P> exec('select * from 一个不在的表')<BR> if @@error<>0<BR> print '这个输出了'<BR> go</P><P> exec sp_executesql N'select * from 一个不在的表'<BR> if @@error<>0<BR> print '这个输出了'</P></PRE></TD></TR></TBODY></TABLE></P>
<P>这样你可以发现通过exec或sp_executesql执行可疑的sql,这样就可以在后面捕捉到被异常终止的错误。</P>
<P><STRONG>二、引出孤立事务</STRONG></P>
<P><STRONG>1、孤立事务的产生</STRONG></P>
<P>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1>
<TBODY>
<TR>
<TD class=code bgColor=#e6e6e6><PRE><P>select @@trancount 当前连接的活动事务数 --当前连接的活动事务数为0 <BR> begin tran</P><P> select * from 一个不在的表<BR> if @@error<>0<BR> begin<BR> print '没有执行到这里来!'<BR> if @@trancount<>0 rollback tran<BR> end</P><P> commit tran</P></PRE></TD></TR></TBODY></TABLE></P>
<P>select @@trancount 当前连接的活动事务数 --执行后你看看 当前连接的活动事务数为1,且重复执行会每次累加,这是很耗资源的。</P>
<P>应为rollback根本就没有被回滚。</P>
<P><STRONG>2、使用现有手段解决孤立事务</STRONG></P>
<P>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1>
<TBODY>
<TR>
<TD class=code bgColor=#e6e6e6><PRE><P>print @@trancount print '当前连接的活动事务数' --当前连接的活动事务数为0 <BR> if @@trancount<>0 rollback tran --在这里写可以让孤立事务只保持到下次你的过程被调用<BR> begin tran</P><P> select * from 一个不在的表<BR> if @@error<>0<BR> begin<BR> print '没有执行到这里来!'<BR> if @@trancount<>0 rollback tran<BR> end</P><P> commit tran</P><P> ---执行后你看看 当前连接的活动事务数为1,但重复执行不会累加</P><P> print @@trancount print '当前连接的活动事务数'</P></PRE></TD></TR></TBODY></TABLE></P>
<P><STRONG>三、使用set xact_abort来控制部分违反约束的错误的执行过程</STRONG></P>
<P>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1>
<TBODY>
<TR>
<TD class=code bgColor=#e6e6e6><PRE><P>create table Table1 (a int check(a>100))<BR> go <BR> set xact_abort on<BR> begin tran<BR> insert table1 values(10)<BR> print '这里没有被执行'<BR> commit tran<BR> go<BR> <BR> print '' print '==============================================' print ''<BR> <BR> set xact_abort off<BR> begin tran<BR> insert table1 values(10)<BR> print '这里被执行'<BR> commit tran</P><P> go<BR> drop table table1</P></PRE></TD></TR></TBODY></TABLE></P>
<P>但 set xact_abort 对于编译产生的错误确没有起作用,且同样会产生孤立事务。</P>
<P>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1>
<TBODY>
<TR>
<TD class=code bgColor=#e6e6e6><PRE><P> set xact_abort on<BR> begin tran<BR> insert 一个不在的表 values(10)<BR> print '这里没有被执行'<BR> commit tran<BR> go</P><P> print '' print '==============================================' print ''</P><P> set xact_abort off<BR> begin tran<BR> insert 一个不在的表 values(10)<BR> print '这里没有被执行'<BR> commit tran<BR> go</P><P> select @@trancount 当前连接的活动事务数 ---有两个孤立事务<BR> if @@trancount<>0 rollback tran</P></PRE></TD></TR></TBODY></TABLE></P>
<P>对于sql中怪辟的各种错误,和孤立事务在t-sql编程中一定要注意,小心孤立事务的陷阱,尽量避免浪费或孤立资源,Microsoft公开宣布过SQLServe下一版本Yukon将有内置异常处理语法。那时可以通过代码对无法预料的错误有更好的控制。</P>
<P><FONT size=4>【相关文章】</FONT></P>
<UL type=disc>
<LI><A href="http://database.51cto.com/art/200707/50713.htm" target=_blank><U><FONT color=blue>精讲SQL Server数据库备份多种方法</FONT></U></A></LI></UL>
<UL type=disc>
<LI><A href="http://database.51cto.com/art/200707/50310.htm" target=_blank><U><FONT color=blue>在SQL Server中如何用通配符搜索TEXT栏</FONT></U></A></LI></UL>
<UL type=disc>
<LI><A href="http://database.51cto.com/art/200706/50241.htm" target=_blank><U><FONT color=blue>如何对SQL Server数据表和数据库进行迭代操作</FONT></U></A></LI></UL>
<DIV align=right>【责任编辑:<A class=ln href="mailto:sunsj@51cto.com">火凤凰</A> TEL:(010)68476606-8007】</DIV></td> </tr> <tr> <td class="d_font4"> </td> </tr> </table>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -