1.异常处理的语法:
引用
BEGIN TRY
{ sql_statement | statement_block }
END TRY
BEGIN CATCH
[ { sql_statement | statement_block } ]
END CATCH
我们将可能会出错的sql 写在begin try...end try 之间,若出错,刚程序就跳到紧接着的begin try...end try 的beign catch...end catch中,执行beign catch...end catch错误处理SQL。try..catch 是可以嵌套的。
2.系统提供的异常处理函数
引用
In the scope of a CATCH block, the following system functions can be used to obtain information about the error that caused the CATCH block to be executed:
ERROR_NUMBER() returns the number of the error.
ERROR_SEVERITY() returns the severity.
ERROR_STATE() returns the error state number.
ERROR_PROCEDURE() returns the name of the stored procedure or trigger where the error occurred.
ERROR_LINE() returns the line number inside the routine that caused the error.
ERROR_MESSAGE() returns the complete text of the error message. The text includes the values supplied for any substitutable parameters, such as lengths, object names, or times.
3.简单例子:
具体参考:
http://msdn.microsoft.com/en-us/library/ms175976.aspx
- 大小: 20 KB
分享到:
相关推荐
TRY…CATCH是Sql Server 2005/2008令人印象深刻的新特性.提高了开发人员异常处理能力.没有理由不尝试一下Try.. Catch功能. * TRY 块 – 包含可能产生异常的代码或脚本 * CATCH 块 – 如果TRY块出现异常,代码处理流将...
SQL Server使用TRY…CATCH 结构实现TSQL语句的错误处理,TRY命令负责监控语句执行的情况,如果有TSQL语句发生异常,并且严重级别(Severity Level)大于10,并且小于20,那么CATCH命令会捕获到异常的错误。...
9.1 SQLServer存储机制 9.1.1 数据库 9.1.2 区段 9.1.3 页 9.1.4 行 9.1.5 稀疏列 9.2 理解索引 9.2.1 平衡树(B.树) 9.2.2 SQLServer中访问数据的方式 9.3 创建、修改和删除索引 9.3.1 CREATEINDEX语句 9.3.2 创建...
import java.sql.*; import jdbc.DBManager; import jdbc.DBManagerTest; /** * <p>Title: * <p>Description: * <p>Copyright: Copyright (c) 2002 * <p>Company: * @author * @version 1.0 */ /** *...
9.1 SQLServer存储机制 9.1.1 数据库 9.1.2 区段 9.1.3 页 9.1.4 行 9.1.5 稀疏列 9.2 理解索引 9.2.1 平衡树(B.树) 9.2.2 SQLServer中访问数据的方式 9.3 创建、修改和删除索引 9.3.1 CREATEINDEX语句 9.3.2 创建...
代码如下: BEGIN TRY … … END TRY BEGIN CATCH DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_...
9.1 SQLServer存储机制 9.1.1 数据库 9.1.2 区段 9.1.3 页 9.1.4 行 9.1.5 稀疏列 9.2 理解索引 9.2.1 平衡树(B.树) 9.2.2 SQLServer中访问数据的方式 9.3 创建、修改和删除索引 9.3.1 CREATEINDEX语句 9.3.2 创建...
基于.net sql server的连接池管理(支持事务管理)类库 轻松实现事务, 示例: public SqlTransaction Transaction { get { return this._transaction; } set { this._transaction = value; userdal....
//conn.ConnectionString = "server=XP-201108091130\\SQLEXPRESS;database=stums;uid=sa;pwd=0"; conn.Open(); SqlTransaction st = conn.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd....
181 9.5.7 Return语句 183 9.5.8 Try...catch语句 185 9.5.9 Execute语句 187 9.6 游标 189 9.6.1 游标简介 189 9.6.2 定义游标 190 9.6.3 打开游标 191 9.6.4 读取游标 192 9.6.5 关闭...
为什么使用事务 当对多... commit transaction end try begin catch select ERROR_NUMBER() as errornumber rollback transaction end catch 隐式设置事务 代码如下: set implicit_transactions on; — 启动隐式事务 g
//捕获执行SQL语句组中的异常 } catch (SQLException e) { try { System.out.println("事务执行失败,进行回滚!\n"); con.rollback(); // 若前面某条语句出现异常时,进行回滚,取消前面执行的所有操作 ...
一款特别好用的.net数据库链接通用类。... SqlTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { foreach (CommandInfo myDE in list) { string cmdText = myDE.CommandText; ...
如果在执行语句在try…catch中,我们可以通过以下方法获取错误信息。sql语句如下: BEGIN TRY SELECT 3 / 0 END TRY BEGIN CATCH DECLARE @errornumber INT DECLARE @errorseverity INT DECLARE...
Statements inside TRY/CATCH blocks are now formatted the same as BEGIN/END blocks Fix for parentheses alignment inside CASE expression (Forum post) 7.3.0.610 - 11th October Experimental feature added ...
sql语句如下: 代码如下:BEGIN TRY SELECT 3 / 0 END TRY BEGIN CATCH DECLARE @errornumber INT DECLARE @errorseverity INT DECLARE @errorstate INT DECLARE @errormessage NVARCHAR(4000) SELECT @errornumber =...
/// <param name="sql">sql语句 /// <returns></returns> public string QuerySingle(string sql) { string single = null; using (OleDbConnection connection = new OleDbConnection(str)) { using ...