SQL Server Can't get multiple SQL WITH statements to work with Azure SQL DB

fd3cxomn  于 2023-04-19  发布在  其他
关注(0)|答案(1)|浏览(131)

I am trying to get multiple WITH statements to work with Azure SQL database, and according to all I found online, this syntax should work, but I get an error:
Msg 102, Level 15, State 1, Line 12
Incorrect syntax near ')'.

SQL:

WITH EpicBenefitsData1 ("Epic Benefits Field Name", "Epic Benefits Field Value", "FK Epic ID") AS 
(
    SELECT 
        "Epic Benefits Field Name", "Epic Benefits Field Value", "FK Epic ID"
    FROM 
        [current_dw].[Epic Benefits] AS EpicBenefits1 
    WHERE 
        EpicBenefits1.[Epic Benefits Field Set Name] = 'Default'
),
EpicBenefitsData2 ("Epic Benefits Field Name", "Epic Benefits Field Value", "FK Epic ID") AS 
(
    SELECT 
        "Epic Benefits Field Name", "Epic Benefits Field Value", "FK Epic ID"
    FROM 
        [current_dw].[Epic Benefits] AS EpicBenefits2 
    WHERE 
        EpicBenefits2.[Epic Benefits Field Set Name] = 'FOOBAR'
)

The error is on the ")" on the last line.

I've tried many different ways, trying UNION between the two, multiple WITHs and not, omitting the first 3 parameters and not, but I always get this error in Azure Data Studio.

Any help would be appreciated. I need to use a PIVOT on the data later, but this part has to work first.

Tried many different variants of WITH syntax, with no luck.

z2acfund

z2acfund1#

You aren't using your CTE.

A CTE on its own is not a valid query, you need to actually select data from your CTE.

with EpicBenefitsData1....
...
select * from EpicBenefitsData1;

相关问题