Incorrect syntax near the keyword 'with'.

后端 未结 3 1977
清酒与你
清酒与你 2021-01-17 11:04

Hello I\'m trying to figure out why switching my compatability mode from 80 to 100 in MSSQL broke my function below?

    Microsoft SQL Server 2008 R2 (RTM)         


        
3条回答
  •  醉酒成梦
    2021-01-17 11:35

    Try throwing a semi colon in front of the with:

    ;with results as
        (
            select parentouid,net_ouid from net_ou where net_ouid=@Param1
            union all
            select t2.parentouid,t2.net_ouid from net_ou t2 
            inner join results t1 on t1.parentouid = t2.net_ouid where t2.parentouid <> t1.net_ouid
        )   
    

    Give this article a read to understand why you need to do that. Snipit:

    However, if the CTE is not the first statement in the batch, you must precede the WITH keyword with a semicolon. As a best practice, I prefer to prefix all of my CTEs with a semicolon—I find this consistent approach easier than having to remember whether I need a semicolon or not.

    Personally, I don't do it for every CTE, but if that makes things easier for you it won't hurt anything.

提交回复
热议问题