首 页 | 精品电影 | 音乐天堂 | 在线游戏 | Flash MTV | 三湘书屋 | 幽默笑话 | 三湘图库 | 美女写真 | IT知识库 | QQ贴图 | 加入书签

网页制作网络编程图形图象操作系统冲浪宝典软件教学网络安全认证考试通信技术电子商务业内动态书籍教程原码

最近更新 文章分类 多媒体类 精品软件

本站搜索:
您的位置:三湘时空 -> IT知识库 -> 文章分类 -> 数据库技巧 -> 一个容易忽视的存储过程问题
一个容易忽视的存储过程问题


文章类别:数据库技巧 来源: 作者: 发表日期:2006-4-23 字体:[ ]

小游戏 | 在线影院 | 幽默笑话 | 源码下载 | Flash MTV | 音乐试听 | 书屋 | 美女写真

sql server 2005中新增加的try catch,可以很容易捕捉异常了,今天大概学习看了下,归纳下要点如下

基本用法BEGIN TRY
     {  sql_statement |
 statement_block  }
END TRY
BEGIN CATCH
     {  sql_statement |
 statement_block }
END CATCH
,和普通语言的异常处理用法差不多,但要注意的是,SQL SERVER只捕捉那些不是严重的异常,当比如数据库不能连接等这类异常时,是不能捕捉的一个例子:BEGIN TRY
  DECLARE @X INT
  -- Divide by zero to generate Error
  SET @X = 1/0
  PRINT 'Command after error in TRY block'
END TRY
BEGIN CATCH
  PRINT 'Error Detected'
END CATCH
PRINT 'Command after TRY/CATCH blocks' 
另外try catch可以嵌套Begin TRY
  delete from GrandParent where Name = 'John Smith'
  print 'GrandParent deleted successfully'
End Try
Begin Catch
   Print 'Error Deleting GrandParent Record'
   Begin Try
     delete from Parent where GrandParentID =
     (select distinct ID from GrandParent where Name = 'John Smith')
     Print 'Parent Deleted Successfully'
   End Try
   Begin Catch
     print 'Error Deleting Parent'
     Begin Try
       delete from child where ParentId =
     (select distinct ID from Parent where GrandParentID =
     (select distinct ID from GrandParent where Name = 'John Smith'))
       print 'Child Deleted Successfully'
     End Try
     Begin Catch
       Print 'Error Deleting Child'
     End Catch
   End Catch
 End Catch
另外,SQL SERVER 2005在异常机制中,提供了error类的方法方便调试,现摘抄如下,比较简单,不予以解释ERROR_NUMBER(): Returns a number associated with the error.ERROR_SEVERITY(): Returns the severity of the error.ERROR_STATE(): Returns the error state number associated with the error.ERROR_PROCEDURE(): Returns the name of the stored procedure or trigger in which the error occurred.ERROR_LINE(): Returns the line number inside the failing 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. 最后举例子如下,使用了error类的方法BEGIN TRY
  DECLARE @X INT
  -- Divide by zero to generate Error
  SET @X = 1/0
  PRINT 'Command after error in TRY block'
END TRY
BEGIN CATCH
  PRINT 'Error Detected'
  SELECT ERROR_NUMBER() ERNumber,
         ERROR_SEVERITY() Error_Severity,
         ERROR_STATE() Error_State,
         ERROR_PROCEDURE() Error_Procedure,
         ERROR_LINE() Error_Line,
         ERROR_MESSAGE() Error_Message
END CATCH
PRINT 'Command after TRY/CATCH blocks'
最后输出Error Detected
Err_Num Err_Sev Err_State Err_Proc             Err_Line  Err_Msg
------- ------- --------- -------------------- --------- --------------------------------
8134        16          1 NULL                 4        Divide by zero error encountered.

出处:jackyrong BLOG

上一篇:sql server 2005中新增的bulk功能 下一篇:服务器安全设置,磁盘权限设置方法
本栏目热门文章
·Windows XP下安装SQL2000企业版 2005-10-4
·在SQL Server 2000里设置和使用数据库复制 2005-11-13
·SQL Server 数据库管理常用的SQL和T-SQL语句 2005-11-13
·SQL SERVER实用技巧 2005-11-3
·配置SQL Server 2000选项 2005-10-8
·如何使Microsoft SQL Server的日志文件不会增大? 2005-10-4
·SQL中通配符、转义符与"["号的使用(downmoon) 2005-11-3
·Sql Server下数据库链接的使用方法 2005-11-21
·使用SQL Server导入和索引 Microsoft Word 文 2005-10-4
·如何快速杀死占用过多资源(CPU,内存)的数据库进程 2005-11-13
新近更新文章
·Oracle:一条SQL实现将多行数据并为一行显示 2006-6-20
·Link Server的语法 2006-6-20
·通过查询分析器对比SQL语句的执行效率 2006-6-15
·问题“未于信任SQL Server连接相关联”的解决 2006-6-15
·数据库死锁导致网站站点访问不了之解决方案 2006-6-15
·在Access中模拟sql server存储过程翻页 2006-6-12
·SQL 存储过程&算法 2006-6-12
·SQL Server 2005 Express附加(Attach)的 2006-6-9
·在SQL Server 2005中实现表的行列转换 2006-6-6
·枚举SQL Server的实例 2006-6-6
首 页 | 软件发布 | 广告联系 | 下载帮助 | 意见反馈 | 网站地图
  CopyRight? 2002-2004 WWW.SXSKY.NET? All Rights Reserved
三湘时空 站长QQ:82675303 Email: