简体   繁体   English

关键字“with”附近的语法不正确。

[英]Incorrect syntax near the keyword 'with'.

Hello I'm trying to figure out why switching my compatability mode from 80 to 100 in MSSQL broke my function below?您好,我想弄清楚为什么在 MSSQL 中将我的兼容性模式从 80 切换到 100 会破坏下面的 function?

    Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (X64)   Apr 22 2011 19:23:43
Copyright (c) Microsoft Corporation  Express Edition with Advanced Services (64-bit) on
Windows NT 6.1 <X64> (Build 7601: Service Pack 1)

Here is my function:这是我的 function:

GO
ALTER FUNCTION [dbo].[GetRoot] 
(
    @Param1 int 
)
RETURNS varchar(50)
AS
BEGIN
DECLARE @ReturnValue varchar(50)
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
    )   
    select @ReturnValue = net_ou.displayname 
    from  NET_OU RIGHT OUTER JOIN
    results ON net_ou.net_ouid = results.ParentouID where results.parentouid=results.net_ouid

    RETURN @ReturnValue

END

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.但是,如果 CTE 不是批处理中的第一个语句,则必须在 WITH 关键字前加上分号。 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.作为最佳实践,我更喜欢在所有 CTE 前加上分号——我发现这种一致的方法比记住是否需要分号更容易。

Personally, I don't do it for every CTE, but if that makes things easier for you it won't hurt anything.就个人而言,我不会为每个CTE 都这样做,但如果这让你的事情变得更容易,那不会有任何伤害。

Add a semicolon before WITH :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
    )   
    select @ReturnValue = net_ou.displayname 
    from  NET_OU RIGHT OUTER JOIN
    results ON net_ou.net_ouid = results.ParentouID where results.parentouid=results.net_ouid

    RETURN @ReturnValue

END

CTE declarations need to be the first command in the batch. CTE 声明必须是批处理中的第一个命令。

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.这是 ANSI 标准的一部分,将在需要处理另一个数据库时为您提供帮助。 SQL Server are moving towards this in any case. SQL 服务器无论如何都朝着这个方向发展。 Many more commands require semicolons now in SQL Server 2012.现在 SQL Server 2012 中的更多命令需要分号。

Eg例如

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.作为一个额外的好处,它使您查询更容易阅读的垃圾负载。 ;-) ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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