国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

sql server 2005中新增加的try catch學(xué)習(xí)

2024-08-31 00:47:50
字體:
供稿:網(wǎng)友

sql server 2005中新增加的try catch,可以很容易捕捉異常了,今天大概學(xué)習(xí)看了下,歸納下要點如下

基本用法begin try
     {  sql_statement |
 statement_block  }
end try
begin catch
     {  sql_statement |
 statement_block }
end catch
,和普通語言的異常處理用法差不多,但要注意的是,sql server只捕捉那些不是嚴(yán)重的異常,當(dāng)比如數(shù)據(jù)庫不能連接等這類異常時,是不能捕捉的一個例子: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類的方法方便調(diào)試,現(xiàn)摘抄如下,比較簡單,不予以解釋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.

中國最大的web開發(fā)資源網(wǎng)站及技術(shù)社區(qū),
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 酉阳| 高邑县| 东乡县| 景泰县| 乐昌市| 朝阳区| 城固县| 工布江达县| 任丘市| 涪陵区| 海盐县| 开平市| 锡林郭勒盟| 菏泽市| 滁州市| 建始县| 长葛市| 大埔区| 浑源县| 巴青县| 盘锦市| 通辽市| 务川| 福贡县| 大安市| 泰宁县| 博白县| 望江县| 烟台市| 尼玛县| 扎兰屯市| 鸡西市| 阿勒泰市| 通城县| 岳西县| 平定县| 孝昌县| 华安县| 拉孜县| 嘉荫县| 盐津县|