Published on

February 13, 2020

Understanding the SET PARSEONLY SQL Command in SQL Server

Developers often write complex and lengthy SQL scripts in SQL Server. During the execution of a SQL query, there are three main phases: parse, compile, and execute. The parse phase checks the syntax of the query and validates variables and identifiers. The compile phase checks for object existence and user permissions, and optimizes the query execution. Finally, the execute phase executes the query and returns the output.

However, there are situations where accidental query execution can cause unintended consequences. For example, pressing F5 by mistake can execute a script that drops tables. To prevent such accidents, SQL Server provides the SET PARSEONLY command.

The SET PARSEONLY command allows developers to execute only the parse phase of a T-SQL query. It can be used in a query batch, and SQL Server will not compile and execute the statement. This can be useful for query debugging or testing purposes.

Here is an example of how to use the SET PARSEONLY command:

SET PARSEONLY ON;
SELECT * FROM [AdventureWorks].[HumanResources].[test];
SET PARSEONLY OFF;

In this example, the query will only be parsed and will not be compiled and executed. This can be helpful when you want to check the syntax of a query without actually executing it.

It’s important to note that the SET PARSEONLY command should be used with caution. It does not support conditional behavior such as CASE and IF statements. Additionally, it can affect the execution of multiple batches in a session. If PARSEONLY is turned on in one batch, it will disable the compile and execute phase for all subsequent batches in the session.

In conclusion, the SET PARSEONLY SQL command in SQL Server allows developers to exclude the compile and execute phase of a query. It can be a useful tool for query debugging and testing purposes. However, it should be used with caution and its impact on multiple batches should be considered.

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.