564 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
How error messages work in SQL (including TRY, CATCH, RAISERROR and THROW) Part three of a four-part series of blogs |
---|
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!
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. |
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.
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. |
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
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!
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.