SQL Server – Difference Between SET NOEXEC ON and SET PARSEONLY ON

sql server

Currently, SQL server supports the following statements: SET NOEXEC and SET PARSEONLY.

From the documentation:

SET NOEXEC ON:

When SET NOEXEC is ON, SQL Server compiles each batch of Transact-SQL statements but does not execute them.

SET PARSEONLY ON:

When SET PARSEONLY is ON, SQL Server only parses the statement.

I guess it's "compiles" vs. "parses". "Compiles" should include "parses".

If I want to verify the correctness of SQL Scripts which one should I use? I am inclined to answer: SET NOEXEC ON.

But what else does compile do? Does "compile" mean the execution plans are created/modified? Is the existence of objects checked?

What happens if you have:

SET NOEXEC ON
SET PARSEONLY OFF

I find having both options available confusing.

Thanks

Best Answer

There are three steps to running a batch:

  1. Parse
  2. Compile
  3. Execute

PARSEONLY stops after step 1). NOEXEC stops after step 2), with the exception that NOEXEC supports deferred name resolution and won't fail if the target tables don't exist.

When SET NOEXEC is ON, SQL Server compiles each batch of Transact-SQL statements but does not execute them.

NOEXEC

When SET PARSEONLY is ON, SQL Server only parses the statement. When SET PARSEONLY is OFF, SQL Server compiles and executes the statement.

PARSEONLY