Incorrect syntax near the keyword 'with'.

后端 未结 3 1938
清酒与你
清酒与你 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:10

    I would suggest that you adopt the practice of ending all statements with a semicolon. This is part of the ANSI standard and will help you when need to work on another database. SQL Server are moving towards this in any case. Many more commands require semicolons now in SQL Server 2012.

    E.g.

    ALTER FUNCTION [dbo].[GetRoot] 
        (@Param1 int)
    RETURNS varchar(50)
    AS
    BEGIN
        DECLARE @ReturnValue VARCHAR(50)
        ;
        WITH cteResults 
        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 )   
        SELECT @ReturnValue = net_ou.displayname 
          FROM net_ou 
         RIGHT JOIN cteResults 
                 ON net_ou.net_ouid = results.ParentouID
         WHERE results.parentouid=results.net_ouid
        ;   
        RETURN @ReturnValue
        ;   
    END
    ;
    GO
    

    As an added bonus it makes you queries a crap load easier to read. ;-)

提交回复
热议问题