RAISERROR
TR A N S A C TI O N S A N D E R R O R H A N D L I N G I N S Q L S E R V E R
Etibar Vazirov
CS instructor at UFAZ
Slides credit: Miriam Antona
Raise errors statements
RAISERROR
THROW
Microsoft suggests THROW
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
RAISERROR syntax
RAISERROR ( { msg_str | msg_id | @local_variable_message },
severity,
state,
[ argument [ ,...n ] ] )
[ WITH option [ ,...n ] ]
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
RAISERROR with message string
IF NOT EXISTS (SELECT * FROM staff WHERE staff_id = 15)
RAISERROR('No staff member with such id.', 16, 1);
Msg. 50000, Level 16, State 1, Line 3
No staff member with such id.
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
RAISERROR with message string
IF NOT EXISTS (SELECT * FROM staff WHERE staff_id = 15)
RAISERROR('No %s with id %d.', 16, 1, 'staff member', 15);
Msg. 50000, Level 16, State 1, Line 3
No staff member with id 15.
RAISERROR('%d%% discount', 16, 1, 50);
Msg. 50000, Level 16, State 1, Line 1
50% discount
Other characters: %i, %o, %x, %X, %u...
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
RAISERROR with error number
RAISERROR(60000, 16, 1);
Msg. 60000, Level 16, State 1, Line 1
This is a test message.
SELECT * FROM sys.messages
| message_id | language_id | severity | is_event_logged | text |
| | | | | |
| ... | ... | ... | ... | ... |
| 60000 | 1033 | 16 | 0 | This is a test message |
| ... | ... | ... | ... | ... |
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
RAISERROR - Example with TRY...CATCH
BEGIN TRY
IF NOT EXISTS (SELECT * FROM staff WHERE staff_id = 15)
RAISERROR('No staff member with such id.', 9, 1);
END TRY
BEGIN CATCH
SELECT 'You are in the CATCH block' AS message
END CATCH
No staff member with such id.
Msg. 50000, Level 9, State 1
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
RAISERROR - Example with TRY...CATCH
BEGIN TRY
IF NOT EXISTS (SELECT * FROM staff WHERE staff_id = 15)
RAISERROR('No staff member with such id.', 16, 1);
END TRY
BEGIN CATCH
SELECT 'You are in the CATCH block' AS message
END CATCH
| message |
| |
| You are in the CATCH block |
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
Let's practice!
TR A N S A C TI O N S A N D E R R O R H A N D L I N G I N S Q L S E R V E R
THROW
TR A N S A C TI O N S A N D E R R O R H A N D L I N G I N S Q L S E R V E R
Etibar
MiriamVazirov
Antona
CS instructor at UFAZ
So ware Engineer
Slides credit: Miriam Antona
THROW syntax
Recommended by Microsoft over the RAISERROR statement.
THROW [ error_number, message, state ][ ; ]
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
THROW - without parameters
BEGIN TRY
SELECT price/0 from orders;
END TRY
BEGIN CATCH
THROW;
SELECT 'This line is executed!' as message;
END CATCH
(0 rows affected)
Msg. 8134, Level 16, State 1, Line 2
Divide by zero error encountered.
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
THROW - ambiguity
BEGIN TRY
SELECT price/0 from orders;
END TRY
BEGIN CATCH
SELECT 'This line is executed!'
THROW;
END CATCH
| THROW |
| |
| This line is executed! |
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
THROW - ambiguity
BEGIN TRY
SELECT price/0 from orders;
END TRY
BEGIN CATCH
SELECT 'This line is executed!';
THROW;
END CATCH
| (No column name) |
| |
| This line is executed! |
(0 rows affected)
(1 rows affected)
Msg. 8134, Level 16, State 1, Line 2
Divide by zero error encountered.
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
THROW - with parameters
THROW error_number, message, state [ ; ]
THROW 52000, 'This is an example', 1;
Msg. 52000, Level 16, State 1, Line 1
This is an example
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
THROW - with parameters
BEGIN TRY
IF NOT EXISTS (SELECT * FROM staff WHERE staff_id = 15)
THROW 51000, 'This is an example', 1;
END TRY
BEGIN CATCH
SELECT ERROR_MESSAGE() AS message;
END CATCH
| THROW |
| |
| This is an example |
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
Let's practice!
TR A N S A C TI O N S A N D E R R O R H A N D L I N G I N S Q L S E R V E R
Customizing error
messages in the
THROW statement
TR A N S A C TI O N S A N D E R R O R H A N D L I N G I N S Q L S E R V E R
Etibar
MiriamVazirov
Antona
CS instructor at UFAZ
So ware Engineer
Slides credit: Miriam Antona
Parameter placeholders in RAISERROR and THROW
RAISERROR('No %s with id %d.', 16, 1, 'staff member', 15);
THROW 52000, 'No staff member with id 15', 1;
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
Ways of customizing error messages
Variable by concatenating strings
FORMATMESSAGE function
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
Using a variable and the CONCAT function
DECLARE @staff_id AS INT = 500;
DECLARE @my_message NVARCHAR(500) =
CONCAT('There is no staff member for id ', @staff_id, '. Try with another one.');
IF NOT EXISTS (SELECT * FROM staff WHERE staff_id = @staff_id)
THROW 50000, @my_message, 1;
Msg. 50000, Level 16, State 1, Line 5
There is no staff member for id 500. Try with another one.
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
The FORMATMESSAGE function
FORMATMESSAGE ( { ' msg_string ' | msg_number } ,
[ param_value [ ,...n ] ] )
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
FORMATMESSAGE with message string
DECLARE @staff_id AS INT = 500;
DECLARE @my_message NVARCHAR(500) =
FORMATMESSAGE('There is no staff member for id %d. %s ', @staff_id, 'Try with another one.');
IF NOT EXISTS (SELECT * FROM staff WHERE staff_id = @staff_id)
THROW 50000, @my_message, 1;
Msg. 50000, Level 16, State 1, Line 6
There is no staff member for id 500. Try with another one.
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
FORMATMESSAGE with message number
SELECT * FROM sys.messages
| message_id | language_id | severity | is_event_logged | text |
| | | | | |
| 60000 | 1033 | 16 | 0 | This is a test message with one numeric parameter (%d)... |
| 21 | 1033 | 20 | 0 | Warning: Fatal error %d occurred at %S_DATE. Note the error...|
| 101 | 1033 | 15 | 0 | Query not allowed in Waitfor. |
| 102 | 1033 | 15 | 0 | Incorrect syntax near '%.*ls'. |
| 103 | 1033 | 15 | 0 | The %S_MSG that starts with '%.*ls' is too long... |
| ... | ... | ... | ... | ... |
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
FORMATMESSAGE with message number
sp_addmessage
msg_id , severity , msgtext,
[ language ],
[ with_log { 'TRUE' | 'FALSE' } ],
[ replace ]
msg_id > 5 0 0 0 0
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
FORMATMESSAGE with message number
TRANSACTIONS AND ERROR HANDLING IN SQL SERVER
Let's practice!
TR A N S A C TI O N S A N D E R R O R H A N D L I N G I N S Q L S E R V E R