Votre propre TRY..CATCH
Modifiez dbo.cuspRideSummaryDelete pour y inclure une erreur volontaire afin de voir comment le bloc TRY CATCH fonctionne.
Cette activité fait partie du cours
Écrire des fonctions et des procédures stockées dans SQL Server
Instructions de l’exercice
- Attribuez par erreur à
@DateParmun type de donnéesnvarchar(30)au lieu dedate. - Incluez
@Errorcomme paramètre facultatifOUTPUT. - Placez l'instruction
DELETEdans le blocBEGIN TRY...END TRY. - Concaténez
ERROR_NUMBER(),ERROR_SEVERITY(),ERROR_STATE(),ERROR_MESSAGE(),ERROR_LINE()dans le blocBEGIN CATCH...END CATCHet faites unSETvers@Error.
Exercice interactif pratique
Essayez cet exercice en complétant ce code d’exemple.
-- 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;