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
Exercise instructions
- Use the
THROWstatement, 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_idin theDECLAREstatement 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_idback 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