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

首頁 > 開發 > 綜合 > 正文

一個容易忽視的存儲過程問題

2024-07-21 02:31:39
字體:
來源:轉載
供稿:網友

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 务川| 广丰县| 涿鹿县| 河北省| 太康县| 铜陵市| 辽宁省| 昌乐县| 彰化市| 堆龙德庆县| 潢川县| 宣威市| 扎兰屯市| 青河县| 稷山县| 三门县| 安仁县| 松潘县| 永春县| 沧州市| 叙永县| 山西省| 两当县| 罗定市| 邮箱| 阿荣旗| 宜春市| 抚顺县| 堆龙德庆县| 虎林市| 通州市| 长丰县| 托克逊县| 弋阳县| 嘉峪关市| 隆安县| 庆阳市| 开平市| 霍城县| 七台河市| 清新县|