LoslegenKostenlos loslegen

Using error functions

For every month, you want to know the total amount of money you earned in your bike store. Instead of reviewing every order line, you thought it would be better to prepare a script that computes it and displays the results.

While writing the script, you made a mistake. As you can see, the operation 'Total: ' + SUM(price * quantity) AS total is missing a cast conversion, causing an error.

How can we catch this error? Show the error number, severity, state, line, and message.

Diese Übung ist Teil des Kurses

Transactions and Error Handling in SQL Server

Kurs anzeigen

Anleitung zur Übung

  • Surround the operation with a TRY block.
  • Surround the functions with a CATCH block.
  • Select the error information.

Interaktive Übung

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

-- Set up the TRY block
___ ___  	
	SELECT 'Total: ' + SUM(price * quantity) AS total
	FROM orders  
___ ___
-- Set up the CATCH block
___ ___  
	-- Show error information.
	SELECT  ___ AS number,  
        	___ AS severity_level,  
        	___ AS state,
        	___ AS line,  
        	___ AS message; 	
___ ___ 
Code bearbeiten und ausführen