Last active
April 16, 2024 00:47
-
-
Save k-maru/68a5b2c27190fc7bbbfb to your computer and use it in GitHub Desktop.
T-SQL Format C# Code
This file contains 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
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); | |
} | |
} |
This file contains 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
Install-Package Microsoft.SqlServer.TransactSql.ScriptDom |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you - really helpful 👍