27

I am looking for a query validator in C#, which allows me to parse the SQL text from a textbox and verify whether it's correct or not before sending it for execution (MS SQL or DB2 queries).

3
  • 1
    MS SQL - is the best query validator. Send the query to it and it will throw exception is the query is wrong. :) Also, the same query can be correct for one DB engine and incorrect for the other one. Why not to let DB engine validate? Commented Jun 9, 2011 at 3:40
  • 1
    ExecuteSQL on MS SQL which would throw a Exception on invalid query ,but potential harm lay wherein these Queries would be fired where as my only intention is to test the Syntax. And what in case of DB2 ? Commented Jun 9, 2011 at 3:43
  • what overhead? DB query validation is one of the very first steps in the query workflow. It will not just validate the syntax, but also check if table or columns exist and all kinds of things like this. Commented Jun 9, 2011 at 3:45

4 Answers 4

38

If you want to validate SQL syntax without the use of a database, the TSql100Parser class will do well for this situation.

Disclaimer, code borrowed from this post here Code to validate SQL Scripts

Pretty straightforward to use though. If it returns null, then there were no errors in parsing it.

using Microsoft.Data.Schema.ScriptDom;
using Microsoft.Data.Schema.ScriptDom.Sql;

public class SqlParser
{
        public List<string> Parse(string sql)
        {
            TSql100Parser parser = new TSql100Parser(false);
            IScriptFragment fragment;
            IList<ParseError> errors;
            fragment = parser.Parse(new StringReader(sql), out errors);
            if (errors != null && errors.Count > 0)
            {
                List<string> errorList = new List<string>();
                foreach (var error in errors)
                {
                    errorList.Add(error.Message);
                }
                return errorList;
            }
            return null;
        }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Am I missing something ? I am not able to get references for Microsoft.Data. I am using VS2010
@Kunal You have to right click on references and add this reference to your project before you can do a using statement to include it.
well I have added both references Microsoft.Data.Schema.ScriptDom and Microsoft.Data.Schema.ScriptDom.Sql, but in using statement it shows error on "Data"and hence not able to get TSql100Parser class.
Any such class to validate DB2 Queries ?
TSql100Parser is part of Microsoft.SqlServer.TransactSql.ScriptDom.dll IScriptFragment with newer VSBuilds is now TSqlFragment
|
16

Set your query to sql with this hint:

set PARSEONLY  on

It just checks your query and returns, like this:

set PARSEONLY  on
select * from tablea

Returns no exception.

While

set PARSEONLY  on
select * f rom tablea

returns

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'f'.

1 Comment

Well. But this one will make a DB call, parse it on Client end and validate SQL text. I want to avoid DB Calls. Also, any such option for DB2 SQLs ?
0

If you would like to validate/parse just a SELECT statement, regardless of how "heavy-duty" that select statement is, I found out that the best and fastest way to validate a select statement is the following: - in your code create 2 select statements (strings) such as:

1) Your valid select statement: SELECT * FROM HUGE_TABLE JOIN MULTIPLE_TABLES WHERE <<Condition>> 2) Create a similar select statement such as SELECT TOP 1 * FROM HUGE_TABLE JOIN MULTIPLE_TABLES WHERE <<Condition>> - Parse/Validate just the second one, regardless of how many joins you have in there, it will parse it in milliseconds, such as:

SqlCommand sqlParse = new SqlCommand(ParseSelectStatement, sqlConn); 

try 
{
sqlConn.Open();
sqlParse.ExecuteNonQuery()
}

Hope it helps! Cheers!

1 Comment

this requires you to run the query if its successful. OP is asking for to "parse the SQL Text from textbox and verify wether its correct or not before sending it for execution" - I understand the next logical step IS to run it but your method is not what OP is asking
-1

I think this is what you are looking for. http://www.codeproject.com/KB/database/sqlvalidator.aspx

2 Comments

This is not easily usable from c#
Cool. But is there anything useful for DB2 Queries ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.