Get startedGet started for free

FORMATMESSAGE with message string

Every time you sell a bike in your store, you need to check if there is enough stock. You prepare a script to check it and throw an error if there is not enough stock.

This exercise is part of the course

Transactions and Error Handling in SQL Server

View Course

Exercise instructions

  • Set @sold_bikes to a value greater than @current_stock (e.g. 100).
  • Customize the error using FORMATMESSAGE with the text 'There are not enough %s bikes. You have %d in stock.' as the first parameter, @product_name as the second parameter, and @current_stock as the third parameter.
  • Pass to the THROW statement the @my_message variable and click Run Code (not Run Solution). You will see the error.
  • Set @sold_bikes in DECLARE statement back to 10. Run the code without errors.

Hands-on interactive exercise

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

DECLARE @product_name AS NVARCHAR(50) = 'Trek CrossRip+ - 2018';
-- Set the number of sold bikes
DECLARE @sold_bikes AS INT = ___;
DECLARE @current_stock INT;

SELECT @current_stock = stock FROM products WHERE product_name = @product_name;

DECLARE @my_message NVARCHAR(500) =
	-- Customize the error message
	FORMATMESSAGE(___, ___, ___);

IF (@current_stock - @sold_bikes < 0)
	-- Throw the error
	THROW 50000, ___, 1;
Edit and Run Code