Published on

August 17, 2011

Structured Error Handling in SQL Server

In everyday life, not everything goes according to plan. The same can be said for working with SQL Server. Sometimes, unexpected errors occur and it’s important to have a structured approach to handle them. This is where structured error handling in SQL Server comes into play.

Structured error handling was introduced in SQL Server 2005 and provides improved options for handling errors. It allows developers to anticipate potential errors and define how SQL Server should behave when it encounters incorrect input.

Let’s take an example from the Grant table. We want to update the GrantName value to NULL, but we expect an error since GrantName is a required field. By using structured error handling, we can catch these types of input errors and provide a more user-friendly experience.

The first step in structured error handling is to use the TRY block. This is where you place the code that may raise an error. Think of the TRY block as “Plan A” – the code that you hope will work on the first try.

If an error occurs within the TRY block, it gets passed to the CATCH block. The CATCH block serves as a contingency plan for failed code from the TRY block. It allows you to handle the error and provide appropriate messaging to the user.

Here’s an example:

BEGIN TRY
    UPDATE [Grant] SET GrantName = '92 Per-cents %% team' WHERE GrantID = '001'
END TRY
BEGIN CATCH
    PRINT 'No Change was made'
END CATCH

In this example, if the UPDATE statement within the TRY block runs successfully, the CATCH block will not be executed. However, if an error occurs, the CATCH block will run and display the error message.

Structured error handling allows you to control and eliminate error messaging, providing a better user experience. It ensures that users receive a message from the application layer, giving them a hint that they’ve entered incorrect input and providing guidance on what the correct input should be.

So, the next time you encounter an error in SQL Server, remember to use structured error handling. It’s like having a Plan B – an alternate route to reach your goal even when things don’t go as planned.

Now, let’s test your knowledge with a quiz:

Question: You have tables named CurrentProducts and SalesInvoiceHeader. The CurrentProducts table has a foreign key relationship with the SalesInvoiceHeader table on the ProductID column. You are deleting ProductID 77 from the Product table and then trying to insert a sale for Product77 into the SalesInvoiceHeader table. What will be the outcome when you run this query?

  1. The product will be deleted from the CurrentProducts table. The order details will be inserted into the SalesInvoiceHeader table.
  2. The product will be deleted from the CurrentProducts table. The order details will not be inserted into the SalesInvoiceHeader table.
  3. The product will not be deleted from the CurrentProducts table. The order details will be inserted into the SalesInvoiceHeader table.
  4. The product will not be deleted from the CurrentProducts table. The order details will not be inserted into the SalesInvoiceHeader table.

Please leave your answer in the comment section below with the correct option, explanation, and your country of residence. Every day, one winner from the United States and one winner from India will be announced. The winners will receive a copy of Joes 2 Pros Volume 4.

Remember, structured error handling is an essential concept in SQL Server. It allows you to handle errors gracefully and provide a better user experience. So, start implementing structured error handling in your SQL Server projects and make your code more robust.

Stay tuned for the next blog post where we will announce the winners and provide the correct answer to the quiz.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.