bruce-dunwiddie / tsql-parser

Library Written in C# For Parsing SQL Server T-SQL Scripts in .Net
Apache License 2.0
324 stars 56 forks source link

Parsing STUFF with SELECT inside throws NULL exception #108

Closed bruce-dunwiddie closed 2 years ago

bruce-dunwiddie commented 2 years ago

Discussed in https://github.com/bruce-dunwiddie/tsql-parser/discussions/107

Originally posted by **cptb** August 3, 2022 Hi Bruce, I am sorry for "always" throwing hard questions at you. This tool is wonderful and I have been using it since last year. Just now, I met a complex query which contains a STUFF function, and inside the function, the first parameter is a SELECT statement to generate some texts. It looks like the below statement. `select stuff ( (select top 10 note from asset_note where note is not null order by note_id for XML path('')) , 1 , 1 , 'Note: ' );` My C# code looks like this: ``` string sql = @"select stuff((select top 10 note from asset_note where note is not null order by note_id for xml path('')), 1, 1 , 'Note: '); "; var statements = TSQLStatementReader.ParseStatements(sql); // this method throws NULL ref exception ``` If the script is simple like this: `SELECT STUFF('SQL Tutorial', 1, 3, 'HTML')` It can be parsed successfully. Could you please look at this issue to see if it is possible to embed SELECT inside the STUFF function? It is valid SQL to use SELECT inside STUFF. Regards Paul
bruce-dunwiddie commented 2 years ago

Resolved in v2.5.0.