Skip to content

Instantly share code, notes, and snippets.

@k-maru
Last active April 16, 2024 00:47
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save k-maru/68a5b2c27190fc7bbbfb to your computer and use it in GitHub Desktop.
Save k-maru/68a5b2c27190fc7bbbfb to your computer and use it in GitHub Desktop.
T-SQL Format C# Code
class Program {
static void Main(string[] args) {
var query = "select A.ID, A.NAME from (select ID, NAME from BAR) A where A.ID = '1'";
var parser = new TSql120Parser(false);
IList<ParseError> errors;
var parsedQuery = parser.Parse(new StringReader(query), out errors);
if (errors.Count > 0) {
foreach (var err in errors) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(err.Message);
Console.ResetColor();
}
}
var generator = new Sql120ScriptGenerator(new SqlScriptGeneratorOptions() {
KeywordCasing = KeywordCasing.Uppercase,
IncludeSemicolons = true,
NewLineBeforeFromClause = true,
NewLineBeforeOrderByClause = true,
NewLineBeforeWhereClause = true,
AlignClauseBodies = false
});
string formattedQuery;
generator.GenerateScript(parsedQuery, out formattedQuery);
Console.WriteLine(formattedQuery);
}
}
Install-Package Microsoft.SqlServer.TransactSql.ScriptDom
@SQL-MisterMagoo
Copy link

Thank you - really helpful 👍

@CarlosV6
Copy link

thanks for you help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment