ComeçarComece de graça

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.

Este exercício faz parte do curso

Transactions and Error Handling in SQL Server

Ver curso

Instruções do exercício

  • 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.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

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
Editar e executar o código