TylerBrinks / SqlParser-cs

A Friendly SQL Parser for .NET
MIT License
103 stars 19 forks source link

Cannot parse t-SQL Statement #43

Closed volodymyr-svystun closed 1 week ago

volodymyr-svystun commented 1 week ago

Hello,

Thanks for your nice utility to work with sql queries. I faced incorrect validation for my query. Let's look at the next code ` var sql = """ SELECT * FROM MyTable WHERE ( (IIF((C0 & CAST(1 AS BIGINT) <> 0), 1, 0)) > 1 ) """;

    using var reader = new StringReader(sql);
    var sqlParser = new TSql120Parser(false); // new TSql160Parser(false) also does not work
    var sqlTokens = sqlParser.Parse(reader, out var errors);`

If I run my sql in management studio it executes without any errors. But sql parser returns 1 error: "Incorrect syntax near ')'." Column: 50 Line: 7 Offset 110

When I validate the same sql only IIF is not in parentheses. it validates without any error: var sql = """ SELECT * FROM MyTable WHERE ( IIF((C0 & CAST(1 AS BIGINT) <> 0), 1, 0) > 1 ) """;

Parentheses are important for my code, so I cannot get rid of them/ Could you please look into this?

TylerBrinks commented 1 week ago

It looks like you're trying to parse ScriptDom or something similar. All the default dialects meet the ANSI/ISO SQL standard which would not validate your script. In order to use alternative dialect features, you would need to implement a custom dialect and use it as an argument then parsing.

https://github.com/TylerBrinks/SqlParser-cs?tab=readme-ov-file#supporting-custom-sql-dialects

volodymyr-svystun commented 1 week ago

@TylerBrinks thank you for helping. Will try to add an own dialect