Skip to content

Null Reference Exception When Parsing Complicated FOR XML #125

@bruce-dunwiddie

Description

@bruce-dunwiddie

Discussed in #124

Originally posted by jim-gregorio February 7, 2023
Hello,

We are using tsql-parser as the foundation for SQL validation. When the following query is parsed it throws a null reference exception.

here is the call stack:

at TSQL.Expressions.Parsers.TSQLArgumentListParser.Parse(ITSQLTokenizer tokenizer)
   at TSQL.Expressions.Parsers.TSQLValueExpressionParser.ParseNext(ITSQLTokenizer tokenizer)
   at TSQL.Expressions.Parsers.TSQLSelectExpressionParser.Parse(ITSQLTokenizer tokenizer)
   at TSQL.Elements.Parsers.TSQLSelectColumnParser.Parse(ITSQLTokenizer tokenizer)
   at TSQL.Clauses.Parsers.TSQLSelectClauseParser.Parse(ITSQLTokenizer tokenizer)
   at TSQL.Statements.Parsers.TSQLSelectStatementParser.Parse()
   at TSQL.TSQLStatementReader.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

This can be reproduced using the following query:

select Stuff((Select ', ' + i.name from sysindexes i where i.id = sys.id order by i.name FOR XML PATH(''),TYPE).value('text()[1]','varchar(max)'),1,2,'') as 'indexes' from sysobjects sys where sys.name like 'DF%'

On SQL Server 2017 or later this can be rewritten to use the string_agg function instead of using for xml. However some servers are 2016 and cannot leverage this function.

Please let me know what you think this can and should be resolved.
Regards.

Metadata

Metadata

Labels

under reviewAttempting to verify and confirm bug

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions