tobymao / sqlglot

Python SQL Parser and Transpiler
https://sqlglot.com/
MIT License
6.74k stars 703 forks source link

Parsing tsql stored proc with declare variable #1966

Closed bayees closed 1 year ago

bayees commented 1 year ago

Error description I get the following sqlglot.errors.ParseError: Invalid expression / Unexpected token Line 4, Col: 28. which is the datatype varchar.

I have look through the library and testet with and without the variable and it might be related to the way that the chunks is made when parsing.

Fully reproducible code snippet

from sqlglot import parse_one, exp

sql = """
CREATE PROC [dbo].[transform_proc] AS 

DECLARE @CurrentDate VARCHAR(20);
SET @CurrentDate = CONVERT(VARCHAR(20), GETDATE(), 120);

CREATE TABLE [target_schema].[target_table]
WITH (DISTRIBUTION = REPLICATE, HEAP)
AS 

SELECT 
    @CurrentDate AS DWCreatedDate
FROM source_schema.sourcetable;
"""

for table in parse_one(sql, dialect='tsql').find_all(exp.Table):
    print(table.db + '.' + table.name)

Official Documentation https://learn.microsoft.com/en-us/azure/synapse-analytics/sql-data-warehouse/sql-data-warehouse-develop-ctas

georgesittas commented 1 year ago

Hey, I can look into this, but don't you need to use the ; separator between the PROC's statements?

bayees commented 1 year ago

Not needed, but updated the query just to be sure. Same resultat though.