開始使用免費開始

CATCH 一個錯誤

執行 dbo.cuspRideSummaryDelete,並將無效的 @DateParm 值 '1/32/2018' 傳入,看看系統如何處理錯誤。這個無效日期會被 @DateParm 的 nvarchar 型別接受,但在執行預存程序時,SQL 嘗試將其轉換為有效日期時就會發生錯誤。

本練習屬於課程

在 SQL Server 中撰寫函式與預存程序

檢視課程

練習說明

  • 使用 DECLARE 宣告變數 @ReturnCode 為 integer,以及 @ErrorOut 為 nvarchar(max)。
  • 執行 dbo.cuspRideSummaryDelete,並將 '1/32/2018' 傳入 @DateParm。
  • 將 @ErrorOut 指派給參數 @Error。
  • 查詢 @ReturnCode 與 @ErrorOut,檢視兩者的值。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

-- Create @ReturnCode
___ ___ ___
-- Create @ErrorOut
___ ___ ___(___)
-- Execute the SP, storing the result in @ReturnCode
___ ___ = ___.___
    -- Specify @DateParm
	___ = '1/32/2018',
    -- Assign @ErrorOut to @Error
	___ = ___ OUTPUT
-- Select @ReturnCode and @ErrorOut
___
	___ AS ReturnCode,
    ___ AS ErrorMessage;
編輯並執行程式碼