klahnakoski / mo-sql-parsing

Let's make a SQL parser so we can provide a familiar interface to non-sql datastores!
Mozilla Public License 2.0
255 stars 58 forks source link

DECLARE throws ParseException (MS SQL Server) #237

Closed a1ea321 closed 3 months ago

a1ea321 commented 4 months ago
from mo_sql_parsing import parse_sqlserver as parse
parse('DECLARE @MYVARIABLE INT = 42;')

results in:

mo_parsing.exceptions.ParseException: Expecting {unsigned} | {zerofill} | {column_options} | {StringEnd} | {lb} | {as}, found "= 42;" (at char 24), (line:1, col:25)

pip show mo-sql-parsing says Version: 10.627.24135

By the way, I'd be honored to submit a fix so I tried. But I got lost around ParseException.bestCause. These two possibilities discourage me:

klahnakoski commented 4 months ago

Good day @a1ea321

For you the fix takes twenty minutes, for me two days.

Yes, you only work on a fix if you love it. Submitting an issue is the fastest way to improve the parser. Faster for you because I know how to fix it. Faster for me because you already know what's wrong.

klahnakoski commented 3 months ago

Here is the fix: https://github.com/klahnakoski/mo-sql-parsing/commit/abf05d6481eed14dd3a147a811eb7cd9945cabb6

The DECLARE only works inside the context of a function/procedure

klahnakoski commented 3 months ago

https://pypi.org/project/mo-sql-parsing/10.642.24144/

a1ea321 commented 3 months ago

Thank you for the fix. (It took me a while to see.)

My real world example is still not working. I checked this page and realized there's more to this issue: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/declare-local-variable-transact-sql?view=sql-server-ver16 Then I added two more tests: PR

There's another case for which I couldn't think of the proper syntax tree, so here's the query: DECLARE @MYTABLEVAR TABLE (id_col INT PRIMARY KEY);