SQL Statement Validator

| |
using System.IO;
using Microsoft.Data.Schema.ScriptDom;
using Microsoft.Data.Schema.ScriptDom.Sql;

public List<string> SQLStmtValid(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;
	}

Reference: http://stackoverflow.com/questions/3276035/code-to-validate-sql-scripts

Originally Posted on June 28, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.