Share this article

Improve this guide

Print Error Message in SQL Server: 3 Ways to Display it

There are several functions you can use to achieve this

2 min. read

Published onNovember 8, 2024

published onNovember 8, 2024

Share this article

Improve this guide

Read our disclosure page to find out how can you help Windows Report sustain the editorial teamRead more

If you’re a developer, you might want to know how to print error messages in SQL Server. This is simpler than you think, and this guide is going to show you a couple of methods you can use.

How can I display an error message in SQL?

How can I display an error message in SQL?

1. Use RAISERROR

Let’s explain all the parameters. Besides your error message, the number 16 describes the severity of an error, and in this case, 16 is used for general user errors.

The last value, 1, indicates a different area of code or state.

As you can see, this method is incredibly simple, and it was used on older SQL servers, before SQL Server 2012.

2. Use THROW

This command is intended for SQL Server 2012 and newer. As for parameters, let’s go through them.

The 50000 is used for error number and for user-defined errors you need to use 50000 or higher. 1 represents state, similar to RAISERROR.

3. Use TRY CATCH

This is how you can print error messages in catch block SQL Server, and this is the best method for debugging your code. Now whenever an error occurs, the ERROR_MESSAGE() will return the error text.

As you can see, it’s pretty simple to print an error message in SQL Server by following these steps.

Now that you know how to handle errors, you should be able to tackle issues such as0x80040e14 SQL Servererror andSQL Server error 18456with ease.

More about the topics:error,sql server

Milan Stanojevic

Windows Toubleshooting Expert

Milan has been enthusiastic about technology ever since his childhood days, and this led him to take interest in all PC-related technologies. He’s a PC enthusiast and he spends most of his time learning about computers and technology.

Before joining WindowsReport, he worked as a front-end web developer. Now, he’s one of the Troubleshooting experts in our worldwide team, specializing in Windows errors & software issues.

User forum

0 messages

Sort by:LatestOldestMost Votes

Comment*

Name*

Email*

Commenting as.Not you?

Save information for future comments

Comment

Δ

Milan Stanojevic

Windows Toubleshooting Expert

Before joining WindowsReport, he worked as a front-end web developer. Now, he’s specialized in Windows errors & software issues.