Get startedGet started for free

THROW with parameters

You need to prepare a script to select all the information of a member from the staff table using a given staff_id.

If the select statement doesn't find any member, you want to throw an error using the THROW statement. You need to warn there is no staff member with such id.

This exercise is part of the course

Transactions and Error Handling in SQL Server

View Course

Exercise instructions

  • Use the THROW statement, with 50001 as the error number, 'No staff member with such id' as the message text, and 1 as the state.
  • Replace the value of @staff_id in the DECLARE statement at the beginning with an identifier that doesn't exist (e.g. '45') and click Run Code (not Run Solution). You will see the error.
  • Set the value of @staff_id back to 4 and run the code without errors.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

DECLARE @staff_id INT = 4;

IF NOT EXISTS (SELECT * FROM staff WHERE staff_id = @staff_id)
   	-- Invoke the THROW statement with parameters
	___ ___, '___', ___;
ELSE
   	SELECT * FROM staff WHERE staff_id = @staff_id
Edit and Run Code