microsoft / SqlScriptDOM

ScriptDOM/SqlDOM is a .NET library for parsing T-SQL statements and interacting with its abstract syntax tree
MIT License
128 stars 13 forks source link

SELECT CAST('blah blah' As int) #50

Closed allnamesaretaken2343 closed 9 months ago

allnamesaretaken2343 commented 12 months ago

Just testing this library, and it just, does not work?

Testing multiple bad SQL... And even the simplest variations go through?

SELECT CAST('blah blah' As int)

This is valid SQL according to Microsoft.SqlServer.TransactSql.ScriptDom ?!

Or am I doing it wrong?

allnamesaretaken2343 commented 12 months ago

Otherwise, errors seem unrelated to actual error...

SELEC CAST('blah blah' As int) Message = "Incorrect syntax near 'blah blah'."

SELECT CAS('blah blah' As int) Message = "Incorrect syntax near As."

Is there a better library out there to parse SQL?

clement911 commented 12 months ago

Yes SELECT CAST('blah blah' As int) is valid at parse time. It only throws an error at execution time.

allnamesaretaken2343 commented 11 months ago

What about the bad error messages?

SELECT CAS('blah blah' As int) Message = "Incorrect syntax near As." More appropriate would be, 'CAS' is not a recognized built-in function name.

I made my own solution, as this library does not produce correct or relevant error messages.

arvindshmicrosoft commented 11 months ago

@allnamesaretaken2343, can you please help us understand your ultimate goal?

As a parser, it is not in the scope of SqlScriptDom to return errors like Conversion failed when converting the varchar value 'blah blah' to data type int when you execute SELECT CAST('blah blah' As int). As @clement911 already mentioned, this statement is 100% valid from a parser perspective. The Error 245 (conversion failed...) that's returned, is part of the SQL execution engine and is out of scope for a parser. This specific example is not a bug at least as far as SqlScriptDom's intended scope is concerned.