BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
You can use BEGIN TRY to trap errors from SQL Server 2005 onwards, and also raise your own errors using RAISERROR or THROW - this blog explains all!
- Error-handling and error messages
- The parts of an error
- A practical example of error-handling (this blog)
- Raising Errors
If you want to learn more about SQL in a classroom setting, you could consider attending a Wise Owl course; otherwise, see the rest of this comprehensive SQL tutorial.
Posted by Andy Brown on 03 May 2013
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
A practical example of error-handling
This page gives a practical example of using TRY / CATCH, which uses the following two tables:
![]() |
![]() |
There are 3 authors ... | ... and 5 books. |
You can run the script to create this database by downloading and opening this file in SQL Server Management Studio.
What our stored procedure should do
Suppose that we want to create a stored procedure to allow us to add a book easily. We could use this to add books for existing authors:
-- author number 3 does exist, so this should work
spAddBook 'The L-Shaped Room', 3
Alternatively, we might try to add books whose authors don't yet exist:
-- there is no author number 4
spAddBook 'The Pelican Brief', 4
Here's what we should see for these two cases:
![]() |
![]() |
If the author exists ... | ... and if the author doesn't. |
Building the stored procedure
The start of the stored procedure should specify which parameters it takes:
-- procedure to insert a book
ALTER PROC spAddBook(
@BookName varchar(100),
@AuthorId int
)
AS
The procedure should then try inserting the book in question, and displaying the record just added if this all goes OK:
BEGIN TRY
-- try inserting book
INSERT INTO tblBook(
BookName,
AuthorId
) VALUES (
@BookName,
@AuthorId
)
-- show last inserted book if worked
SELECT TOP 1 * FROM tblBook
ORDER BY BookId DESC
END TRY
If this fails (perhaps because the author number given doesn't exist in the authors table, so that the referential integrity constraint prevents insertion of the book), control will jump to the CATCH clause:
BEGIN CATCH
-- if couldn't insert book, show why
SELECT
'Could not do' AS ErrorMessage,
ERROR_NUMBER() AS 'Error no',
'Author ' + CAST(@AuthorId AS varchar(10)) + ' not found' AS Problem
END CATCH
Running the stored procedure
Here are two calls to this stored procedure (the first will insert a row, the second will display the error details):
-- add a book by Lynne Reid Banks
spAddBook 'The L-Shaped Room', 3
-- there is no author 5 yet!
spAddBook 'The Lacuna', 5
As the final part of this blog, I'll now take a look at raising your own errors!
- Error-handling and error messages
- The parts of an error
- A practical example of error-handling (this blog)
- Raising Errors