LoslegenKostenlos starten

Your very own TRY..CATCH

Alter dbo.cuspRideSummaryDelete to include an intentional error so we can see how the TRY CATCH block works.

Diese Übung ist Teil des Kurses

<Kurs>Writing Functions and Stored Procedures in SQL Server</Kurs>
Kurs ansehen

Übungsanweisungen

  • Incorrectly assign @DateParm a nvarchar(30) data type instead of a date.
  • Include @Error as an optional OUTPUT parameter.
  • Include the DELETE statement within the BEGIN TRY...END TRY block.
  • Concatenate the ERROR_NUMBER(), ERROR_SEVERITY(), ERROR_STATE(), ERROR_MESSAGE(), ERROR_LINE() within the BEGIN CATCH...END CATCH block and SET to @Error.

Interaktive praktische Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

-- Alter the stored procedure
CREATE OR ALTER PROCEDURE dbo.cuspRideSummaryDelete
	-- (Incorrectly) specify @DateParm
	___ ___(30),
    -- Specify @Error
	___ nvarchar(max) = NULL OUTPUT
AS
SET NOCOUNT ON
BEGIN
  -- Start of the TRY block
  ___ ___
  	  -- Delete
      DELETE FROM RideSummary
      WHERE Date = @DateParm
  -- End of the TRY block
  ___ ___
  -- Start of the CATCH block
  ___ ___
		SET @Error = 
		'Error_Number: '+ CAST(___() AS VARCHAR) +
		'Error_Severity: '+ CAST(___() AS VARCHAR) +
		'Error_State: ' + CAST(___() AS VARCHAR) + 
		'Error_Message: ' + ___() + 
		'Error_Line: ' + CAST(___() AS VARCHAR)
  -- End of the CATCH block
  ___ ___
END;
Code bearbeiten und ausführen