Home > OS >  Why the AST created by Microsoft.SqlServer.Management.SqlParser.Parser for this query contains SqlTa
Why the AST created by Microsoft.SqlServer.Management.SqlParser.Parser for this query contains SqlTa

Time:09-16

Please, observe the following simple query:

SELECT ShortName FROM Groups (nolock)

I parse it like this:

var parseResult = Parser.Parse("SELECT ShortName FROM Groups (nolock)");
Dump(parseResult.Script);

...

private static void Dump(string indent, SqlCodeObject sqlCodeObject)
{
    Console.WriteLine(indent   sqlCodeObject.GetType().Name);
    indent  = "  ";
    foreach (var child in sqlCodeObject.Children)
    {
        Dump(indent, child);
    }
}

This outputs the following AST:

SqlScript
  SqlBatch
    SqlSelectStatement
      SqlSelectSpecification
        SqlQuerySpecification
          SqlSelectClause
            SqlSelectScalarExpression
              SqlColumnRefExpression
                OnePartObjectIdentifier
                  SqlIdentifier
          SqlFromClause
            SqlTableValuedFunctionRefExpression
              OnePartObjectIdentifier
                SqlIdentifier
              SqlColumnRefExpression
                OnePartObjectIdentifier
                  SqlIdentifier

My question - why is SqlFromClause parent to SqlTableValuedFunctionRefExpression? I do not have any function calls in the query.

CodePudding user response:

This is the only combination of settings I could get the desired result from using SMO 150.18208.0:

var opts = new ParseOptions("GO", true, DatabaseCompatibilityLevel.Version80);
opts.TransactSqlVersion = TransactSqlVersion.Version105;

var parseResult = Parser.Parse("SELECT ShortName FROM Groups (nolock)", opts);
Dump("  ",parseResult.Script);

outputs

SqlScript
    SqlBatch
      SqlSelectStatement
        SqlSelectSpecification
          SqlQuerySpecification
            SqlSelectClause
              SqlSelectScalarExpression
                SqlColumnRefExpression
                  OnePartObjectIdentifier
                    SqlIdentifier
            SqlFromClause
              SqlTableRefExpression
                OnePartObjectIdentifier
                  SqlIdentifier
                SqlTableHint
  • Related