Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically check (parse) the validity of a TSQL statement?

I'm trying to make my integration tests more idempotent. One idea was to execute rollback after every test, the other idea was to some how programatically parse the text, similar to the green check box in Query Analyzer or SSMS.

How do I get SQL Server to parse my command without running it using ADO.NET?

UPDATE: This is what finally worked as desired:

using (DbCommand executeOnly = Factory.DbCommand()) {     executeOnly.Connection = command.Connection;     executeOnly.CommandType = CommandType.Text;     executeOnly.CommandText = "SET NOEXEC ON;" + sqlCommand;     executeOnly.Connection.Open();     executeOnly.ExecuteNonQuery(); } //set more properties of command. command.Execute(); 

For inexplicable reasons, "SET PARSEONLY ON" only worked in Query Analyzer. I couldn't set this on an ADO.NET connection. It is just as well because PARSEONLY seems to catch only syntax errors, which isn't a common error. SET NOEXEC ON will catch a wider varieties of errors, such as a view that references a missing table or column or a missing parameter in a stored procedure.

like image 777
MatthewMartin Avatar asked Jun 21 '10 11:06

MatthewMartin


People also ask

What is Parse command in SQL?

PARSE() function in SQL Server PARSE() function can convert any string value to Numeric or Date/Time format. If passed string value cannot be converted to Numeric or Date/Time format, it will result to an error. PARSE() function relies on Common Language Runtime to convert the string value.

How SQL queries are parsed?

SQL Parsing The parsing stage involves separating the pieces of a SQL statement into a data structure that other routines can process. The database parses a statement when instructed by the application, which means that only the application, and not the database itself, can reduce the number of parses.

What is query parsing in SQL Server?

Parsing of a query is the process by which this decision making is done that for a given query, calculating how many different ways there are in which the query can run. Every query must be parsed at least once. The parsing of a query is performed within the database using the Optimizer component.

What statement can used to check existing database?

The SQL USE statement is used to select any existing database in the SQL schema.


2 Answers

I think the command you are looking for is SET NOEXEC ON. If you set this for your connection, the queries will be parsed but will not be executed. Another option would be SET PARSEONLY ON, but I'm honestly not sure what the difference between the two really is.

like image 68
Eric Petroelje Avatar answered Sep 22 '22 01:09

Eric Petroelje


+1 to Eric's answer. But I've found SET FMTONLY ON to also be useful as SET NOEXEC ON doesn't appear to throw up all errors.

e.g.

SELECT * FROM ATableThatDoesNotExist 

Running that with SET NOEXEC ON says it was successful, despite the table not existing in the database. Running it with SET FMTONLY ON instead, will throw the "Invalid object name" error.

SET FMTONLY ON also returns metadata about the resultset that would be returned, which can come in very handy

like image 30
AdaTheDev Avatar answered Sep 25 '22 01:09

AdaTheDev