Method for verifying SQL statements in C # (set parseonly and set noexec)

Source: Internet
Author: User

Set parseonly
Check the syntax of each Transact-SQL statement and return any error message without compiling or executing the statement.
Set parseonly {on | off}
When set parseonly is on, SQL Server only analyzes statements. When set parseonly is off, SQL Server compiles and runs the statement.
Set parseonly is set during analysis, rather than during execution or runtime.
Do not use parseonly in stored procedures or triggers. If the offsets option is on and no error occurs, set parseonly returns the offset.

Set noexec (TRANSACT-SQL)
Compile each query without executing the query.
Set noexec {on | off}
When set noexec is on, SQL Server compiles and processes each batch of transact-SQL statements but does not execute them. When set noexec is set to off, all batch processing is performed after compilation.
SQL Server contains two phases: Compilation and execution. This setting allows SQL Server to verify the syntax and object name in the code when executing the transact-SQL code. It can also be used to debug some statements that are usually in a large batch.

Set noexec settings are set during execution or runtime, rather than during analysis.

Set parseonly code:

public bool ValidateSQL(string sql){bool bResult;SqlCommand cmd = _conn.CreateCommand();cmd.CommandText = "SET PARSEONLY ON";cmd.ExecuteNonQuery();try{cmd.CommandText = sql;cmd.ExecuteNonQuery();bResult = true;}catch (Exception ex){bResult = false;}finally{cmd.CommandText = "SET PARSEONLY OFF";cmd.ExecuteNonQuery();}return bResult;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.