“与” ......一语句中的关键字附近的不正确语法必须以分号结束(Incorrect syntax

2019-07-18 08:43发布

即时通讯使用的SQL Server 2005。 我有2个在我的存储过程条款

WITH SomeClause1 AS
(
  SELECT ....
)
WITH SomeClause2 AS
(
  SELECT ....
)

但出现错误

“与”关键字附近的语法不正确。 如果这种说法是公用表表达式或XMLNAMESPACES子句,前面的语句必须以分号结束。

我有哪些选择? 是否有任何分路器,我不知道?

Answer 1:

用逗号分隔的CTE

;WITH SomeClause1 AS
(
  SELECT ....
)
, SomeClause2 AS
(
  SELECT ....
)


Answer 2:

忘记添加一个“;” 在前面的语句,如错误信息说。 刚拿到的总是编码它喜欢的习惯:“WITH”,你会没事的......

;WITH SomeClause1 AS
(
  SELECT ....
)

但是,你必须连接用逗号多个热膨胀系数,但是“WITH”始终有一个分号之前:

;WITH SomeClause1 AS
(
  SELECT ....
)
,SomeClause2 AS
(
  SELECT ....
)


Answer 3:

姆拉登Prajdic建议以此为“WITH XMLNAMESPACES”的解决方案,工程巨大。

http://itknowledgeexchange.techtarget.com/sql-server/using-xmlnamespaces-within-a-function/



Answer 4:

对我不起作用。

就我而言,我使用一个表值用户定义函数的返回子句中的CTE值。 如果我的包裹在RETURN子句BEGIN-END我得到同样的错误信息,但裸RETURN()子句工作好。 我相信错误消息在这种情况下不正确。

这工作:

CREATE FUNCTION [dbo].[ft_SplitStringOnChar]
      (
      @s varchar(8000),
      @sep char(1)
      )

RETURNS TABLE
AS

RETURN (
    WITH Pieces(pn, start, stop) AS (
      SELECT 1, 1, CHARINDEX(@sep, @s)
      UNION ALL
      SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
      FROM Pieces
      WHERE stop > 0
    )
    SELECT pn AS TokenNumber,
      SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS TokenString
    FROM Pieces
  )

GO  

This does not:

CREATE FUNCTION [dbo].[ft_SplitStringOnChar]
      (
      @s varchar(8000),
      @sep char(1)
      )

RETURNS TABLE
AS
BEGIN
;
RETURN (
    WITH Pieces(pn, start, stop) AS (
      SELECT 1, 1, CHARINDEX(@sep, @s)
      UNION ALL
      SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
      FROM Pieces
      WHERE stop > 0
    )
    SELECT pn AS TokenNumber,
      SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS TokenString
    FROM Pieces
  )
END
GO  


文章来源: Incorrect syntax near the keyword 'with'…previous statement must be terminated with a semicolon