From the course: Complete Guide to Advanced SQL Server
Unlock the full course today
Join today to access over 24,400 courses taught by industry experts.
Generate errors with THROW - SQL Server Tutorial
From the course: Complete Guide to Advanced SQL Server
Generate errors with THROW
Sometimes SQL Server is perfectly happy executing an instruction that doesn't actually make sense for your specific database. For instance, a user sends a command to perform a task that's technically valid SQL, but the end result doesn't correspond to the business rules that your organization needs to follow. In these cases, you can create your own custom error messages using a command called THROW. The nice thing about THROW is that you can insert it anywhere that you want. It doesn't have to be attached to any actual database or server errors. To see how this can be helpful, I've got a TRY-CATCH statement set up here on Lines 7 through 13. The first thing that I'm doing is requesting a row from the people's table. Now, this SELECT statement should work just fine and is not going to generate any errors. Then on Line Number 9, we have our THROW command which will create an error that transfers execution into the CATCH block. The THROW command has three parts to it. First, we have a…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
What is error handling?1m 52s
-
(Locked)
Capture errors with TRY and CATCH4m 54s
-
(Locked)
Generate errors with THROW4m 12s
-
(Locked)
Manage transaction control4m 4s
-
(Locked)
Challenge: Add error handling to a stored procedure1m 12s
-
(Locked)
Solution: Add error handling to a stored procedure12m 12s
-
(Locked)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-