简体   繁体   中英

Syntax for CTE in Microsoft SQL Server Management Studio

;WITH 
cte_REFERRALS_REPORTS (referralnum, refer_from, refer_from_name, refer_from_id, refer_to, refer_to_name, refer_to_id)
AS
    (
    SELECT 
        referralnum, refer_from,
        CASE
                WHEN refer_from_id = 'R'    THEN RdicF.refname 
                WHEN refer_from_id = 'P'    THEN PdicF.provname END AS refer_from_name,
        refer_from_id, refer_to, 
        CASE
                WHEN refer_to_id = 'R'      THEN RdicT.refname
                WHEN refer_to_id = 'P'      THEN PdicT.provname END AS refer_to_name,
        refer_to_id
    FROM 
        referral_t r
    LEFT JOIN 
        refcode_t RdicF
    ON  r.refer_from = CASE WHEN r.refer_from_id='R' THEN RdicF.refcode ELSE NULL END
    LEFT JOIN 
        refcode_t RdicT
    ON  r.refer_to = CASE WHEN r.refer_to_id = 'R' THEN RdicT.refcode ELSE NULL END
    LEFT JOIN
        provcode_t PdicF
    ON r.refer_from  = CASE WHEN r.refer_from_id = 'P' THEN PdicF.provcode ELSE NULL END
    LEFT JOIN
        provcode_t PdicT
    ON r.refer_to = CASE WHEN r.refer_to_id = 'P' THEN PdicT.provcode ELSE NULL END
    )

My Query inside CTE works but the syntax is wrong I feel. I keep getting this error:

"Incorrect syntax near ')'"

Please help. I am new to SQL and coding. I am struggling with CTE and Window functions.

You're missing the SELECT (or UPDATE, DELETE or INSERT) that uses the CTE. There's no sense on declaring a CTE that won't be used in the statement.

WITH 
cte_REFERRALS_REPORTS (referralnum, refer_from, refer_from_name, refer_from_id, refer_to, refer_to_name, refer_to_id)
AS
    (
    SELECT 
        referralnum, refer_from,
        ISNULL( RdicF.refname, PdicF.provname) END AS refer_from_name, --Code shortened, but you could keep the original for clarity.
        refer_from_id, refer_to, 
        CASE
                WHEN refer_to_id = 'R'      THEN RdicT.refname
                WHEN refer_to_id = 'P'      THEN PdicT.provname END AS refer_to_name,
        refer_to_id
    FROM      referral_t r
    LEFT JOIN refcode_t RdicF ON  r.refer_from = RdicF.refcode  AND r.refer_from_id = 'R'
    LEFT JOIN refcode_t RdicT ON  r.refer_to   = RdicT.refcode  AND r.refer_to_id = 'R'
    LEFT JOIN provcode_t PdicF ON r.refer_from = PdicF.provcode AND r.refer_from_id = 'P'
    LEFT JOIN provcode_t PdicT ON r.refer_to   = PdicT.provcode AND r.refer_to_id = 'P'
    )
SELECT *
FROM cte_REFERRALS_REPORTS;

I made some changes to your code that might help developers and the optimizer to understand what's happening on those JOINS. You also need to understand that the semicolon (;) is a statement terminator and should be at the end of each statement and not at the beginning of a CTE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM