Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSSQL: Issue with CTE anchors #1537

Open
kasvith opened this issue Nov 20, 2024 · 0 comments
Open

MSSQL: Issue with CTE anchors #1537

kasvith opened this issue Nov 20, 2024 · 0 comments

Comments

@kasvith
Copy link

kasvith commented Nov 20, 2024

Hi, the following is a valid MSSQL syntax

When parsed with the sql-parser-rs

WITH RecursiveCTE AS (
    -- Anchor member
    SELECT TOP 1
        [ID],
        CAST([Amount] AS NUMERIC) AS [Amount],
        CAST([Balance] AS NUMERIC) AS [Balance],
        (SELECT TOP 1 [FixedValue] FROM [Settings] ORDER BY [ID] DESC) AS [Constant]
    FROM [Transactions]
    ORDER BY [ID] ASC

    UNION ALL

    -- Recursive member
    SELECT
        [ID] + 1 AS [ID],
        CAST([Amount] * 0.8 AS NUMERIC) AS [Amount],
        CAST(([Amount] * 0.8 - [Constant]) AS NUMERIC) AS [Balance],
        [Constant]
    FROM RecursiveCTE
    WHERE [Balance] > 0
)
SELECT [ID], [Balance]
FROM RecursiveCTE
ORDER BY [ID] ASC;

it gives the following error

sql parser error: Expected: ), found: UNION at Line: 11, Column: 5

https://learn.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql

@kasvith kasvith changed the title MSSQL: Issue with UNION ALL MSSQL: Issue with CTEs Nov 20, 2024
@kasvith kasvith changed the title MSSQL: Issue with CTEs MSSQL: Issue with CTE anchors Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant