Created
March 15, 2021 04:40
-
-
Save jtlimson/3f2a94b97aac13fa612728b160239693 to your computer and use it in GitHub Desktop.
Check if SQL string syntax valid.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.SqlServer.TransactSql.ScriptDom; | |
/// <summary> | |
/// Install-Package Microsoft.SqlServer.TransactSql.ScriptDom -Version 14.0.3811. | |
/// </summary> | |
namespace ApplicationLayout.Areas.RemQuery.Models | |
{ | |
public class SQLParser | |
{ | |
public bool IsSQLQueryValid(string sql, out List<string> errors) | |
{ | |
errors = new List<string>(); | |
TSql140Parser parser = new TSql140Parser(false); | |
TSqlFragment fragment; | |
IList<ParseError> parseErrors; | |
using (TextReader reader = new StringReader(sql)) | |
{ | |
fragment = parser.Parse(reader, out parseErrors); | |
if (parseErrors != null && parseErrors.Count > 0) | |
{ | |
errors = parseErrors.Select(e => e.Message).ToList(); | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment