当前位置:网站首页>SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view

SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view

2022-04-23 16:51:00 Tassel 1990

Reprint :https://www.andriescu.nl/sql/sql-how-to-parse-microsoft-transact-sql-statements-in-c_sharp_view_column_binding/

public static TSqlScript Convert(string sql, out string prettySql)
{
    var hasQuotedIdentifiers = false;
    var parser = new TSql100Parser(hasQuotedIdentifiers);
    var options = new SqlScriptGeneratorOptions();
    options.SqlVersion = SqlVersion.Sql100;
    options.KeywordCasing = KeywordCasing.Uppercase;
    options.MultilineSelectElementsList = true;
    options.MultilineWherePredicatesList = true;
 
    IScriptFragment fragment;
    IList<ParseError> errors;
    using (var sr = new StringReader(sql))
    {
        fragment = parser.Parse(sr, out errors);
    }
 
    if (errors != null && errors.Count > 0)
    {
        var errorMessage = new StringBuilder();
        foreach (var error in errors)
        {
            errorMessage.Append(error.Message);
            errorMessage.Append("offset " + error.Offset);
        }
 
        throw new ApplicationException(errorMessage.ToString());
    }
 
    new Sql100ScriptGenerator(options).GenerateScript(fragment, out prettySql);
 
    var parsedSql = fragment as TSqlScript;
 
    return parsedSql;
}

 

版权声明
本文为[Tassel 1990]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231359254009.html