Rajendra Gupta

SQL Server PRINT and SQL Server RAISERROR statements

October 2, 2019 by

This article explores the SQL Server PRINT statements, and its alternative SQL Server RAISEERROR statements to print messages in a query.

Introduction

Suppose you execute a query with multiple steps. We want to get a message once each step completes. It helps to track the query progress. Usually, we use the SQL PRINT statement to print corresponding messages or track the variable values while query progress.

We also use interactions or multiple loops in a query with a while or for a loop. We can also use the SQL PRINT statement to track the iteration.

We use the SQL Server PRINT statement to return messages to the client. We specify the message as string expressions input. SQL Server returns the message to the application.

In this article, we will explore several use cases of SQL PRINT statement, its limitations, and alternatives of SQL PRINT statements.

Example 1: SQL Server PRINT statement to print a string

It is the simplest example. Execute the following query in SSMS, and it returns the following message in the output:

PRINT statement to print a string

In SSMS, we get the PRINT statement output in SSMS message window as shown in the above image.

Example 2: PRINT statement to print a variable value

We can use the SQL PRINT statement to print a variable value as well. Let’s define a message in a variable and later print this message:

PRINT statement to print a variable value

Example 3: SQL Server PRINT statement to print an integer value

We can use the SQL PRINT statement to print an integer value, as shown below:

PRINT statement to print an integer value

We can specify only CHAR, NCHAR, VARCHAR or NVARCHAR data types in the PRINT statement. In this case, it implicitly converts an integer value to the VARCHAR data type internally.

Let’s use another example with an integer variable and a string in the PRINT statement. You get an error message in data type conversion because SQL Server is trying to convert the varchar data type to integer. In data type precedence, the integer data type has high precedence than the varchar data type:

Conversion failure message

We explicitly convert the integer data type to varchar using the SQL CAST statement:

explicitly convert data type

We can use the SQL CONCAT function as well, and it automatically does data type conversion for us. In the following query, we get the output using the CONCAT function similar to the CAST operator:

SQL CONCAT

Example 4: SQL Server PRINT statement with XML type variable value

We can use XML data type as well with the PRINT statement, but it requires data conversion.

As shown in the following output, we cannot directly use an XML variable in the PRINT statement. It gives an error message that implicit conversion from XML to nvarchar is not allowed:

PRINT statement with XML type variable value

We can use SQL CAST or CONVERT function explicitly and get the required output:

PRINT statement with XML type variable value

Example 5: SQL Server PRINT Statement with IF conditions

Let’s use the PRINT statement to print the message satisfied in the IF condition. In this example, the variable @a contains a string. The IF condition checks for the string and prints message satisfying the condition:

PRINT Statement with IF conditions

Example 6: PRINT Statement with NULL values

We cannot print NULL in the message using the SQL PRINT statement. The following query does not return any result:

 PRINT Statement with NULL values

Let’s use the following query that contains a variable with NULL values. In the PRINT statement, we use a string along with this variable, and it does not return any message. The concatenation of a string and the variable @a (NULL) that does not return any output:

 PRINT Statement with NULL values

Example 7: SQL Server PRINT Statement in a WHILE loop

As stated earlier, many times, we require knowing information about each iteration when query running in a loop such as WHILE or FOR.

The following query uses WHILE loop and prints a message about each iteration:

It gives the following output. We can use a Print statement with an appropriate message and track query progress.

PRINT Statement in a WHILE loop

Limitations of SQL Server PRINT statement

In the previous example, we saw the use of cases of SQL PRINT statements. We have the following limitations with PRINT as well:

  1. We need to use CAST, CONVERT or CONCAT functions in the PRINT statement with variables
  2. We cannot see a PRINT statement in SQL PROFILER
  3. The PRINT statement does not return the message immediately; it buffers the output and displays them

Let’s elaborate point no 3 and see its alternatives.

Execute the following query that contains two PRINT statements. Looking at the query, you might think of output in the following form:

  • It gives the message from the first PRINT statement
  • Waits for 5 seconds
  • It gives the message for a second PRINT statement
  • Waits for another 5 seconds

In the following GIF image, you can note that it prints message from both SQL PRINT statements together after 10 seconds:

PRINT Statement in a WHILE loop

Let’s use another example and see PRINT statement behavior. In this, we want to print the message as soon as iteration completes:

In the output, we can note that all messages output from PRINT statements appear together once the execution completes. The loop executes 14 times and waits 1 second on each execution. Therefore, we get output after 14 seconds for all PRINT statements:

PRINT Statement in a WHILE loop

SQL Server buffers messages for multiple PRINT statements and displays them together.

It is not the desired output, and in any case, it might not be useful as you cannot track query progress in real-time.

SQL Server RAISERROR statement

We can use an alternative to the SQL PRINT statement that is RAISERROR.

We require a few arguments in RAISERROR statements.

  • Message – It is the message that we want to print
  • Severity – It is a number between 0 and 25 and defines the severity of the messages. It treats the message differently with each severity. We will see a few examples of it in this article
  • State – It is a number between 0 and 255 to distinguish one error from another. It is good to use value 1 for this article

We need to use RAISERROR statement with NOWAIT clause; otherwise, it shows the same behavior as of SQL PRINT statement:

In the following output with SQL Server RAISERROR statements, we can note the following:

  • It gives the message from the first PRINT statement
  • Waits for 5 seconds
  • It gives the message for the second PRINT statement
  • Waits for another 5 seconds

PRINT Statement in a WHILE loop

Previously we used severity 0 in the RAISERROR statement. Let’s use the same query with severity 16:

In the output, we can see the message appears in red, and it shows the message as an error instead of a regular message. You also get message id, level, and state as well:

Severity 16 in SQL Server RAISERROR

Let’s execute the following query with severity 1 and severity 16. Severity 1 shows the message with additional information, but it does not show the message as an error. You can see the text color in black.

Another SQL Server RAISERROR shows the output message as an error:

Severity 16 and 1 in RAISERROR

We cannot use SQL Server RAISERROR directly using the variables. We get the following output that is not the desired output:

It prints the message but does not show the value of the variable:

variable with RAISERROR

We need to use the C-style print statements with RAISERROR. The following query shows the variable with the RAISERROR. You can notice that we use %s and %d to print a string and integer value:

We get the instant output in SQL Server RAISERROR along with WITH NOWAIT statement as per our requirement and does not use buffer to display output once the query finishes:

PRINT Statement in a WHILE loop

You might confuse between RAISERROR statement that it is for raising error messages in SQL Server. We can use it as an alternative to the SQL PRINT statement as well. Usually, developers use PRINT statements only to gives messages in a query. You should explore RAISERROR statements for your queries, stored procedures.

Conclusion

In this article, we explored the SQL Server PRINT statement and its usages to track useful milestones in a query. We also learned the limitations of it along with alternative solution RAISERROR statement in SQL Server. SQL Server RAISERROR gives you a great advantage to control output buffer behavior.

Rajendra Gupta

Rajendra Gupta

Rajendra has 8+ years of experience in database administration having a passion for database performance optimization, monitoring, and high availability and disaster recovery technologies, learning new things, new features.

While working as a Senior consultant DBA for big customers and having certified with MCSA SQL 2012, he likes to share knowledge on various blogs.
He can be reached at rajendra.gupta16@gmail.com

View all posts by Rajendra Gupta
Rajendra Gupta

Latest posts by Rajendra Gupta (see all)

156 Views