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).
-
1MS 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?Alex Aza– Alex Aza2011-06-09 03:40:50 +00:00Commented Jun 9, 2011 at 3:40
-
1ExecuteSQL 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 ?Cannon– Cannon2011-06-09 03:43:23 +00:00Commented 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.Alex Aza– Alex Aza2011-06-09 03:45:12 +00:00Commented Jun 9, 2011 at 3:45
4 Answers
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;
}
}
9 Comments
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
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
I think this is what you are looking for. http://www.codeproject.com/KB/database/sqlvalidator.aspx