分割函数出现错误

发布于 2025-01-01 08:47:54 字数 781 浏览 0 评论 0原文

下面的代码我用于 MSSQL 2005,但是当我尝试在 MSSQL 2000 中运行时,我遇到了以下错误。

Msg 156, Level 15, State 1, Procedure StrSplit, Line 5
Incorrect syntax near the keyword 'WITH'.
Msg 170, Level 15, State 1, Procedure StrSplit, Line 15
Line 15: Incorrect syntax near ')'.

CREATE FUNCTION dbo.StrSplit (@sep char(1), @s varchar(512))  
    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,  
          SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s  
        FROM Pieces  
      )

提前致谢

Below code i am using for MSSQL 2005,but when i try to run in MSSQL 2000 i am getting below erros.

Msg 156, Level 15, State 1, Procedure StrSplit, Line 5
Incorrect syntax near the keyword 'WITH'.
Msg 170, Level 15, State 1, Procedure StrSplit, Line 15
Line 15: Incorrect syntax near ')'.

CREATE FUNCTION dbo.StrSplit (@sep char(1), @s varchar(512))  
    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,  
          SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s  
        FROM Pieces  
      )

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巾帼英雄 2025-01-08 08:47:54

使用以下函数代替它。

CREATE  FUNCTION [dbo].[fn_Split](@text VARCHAR(8000), @delimiter VARCHAR(20) = ' ')
RETURNS @Strings TABLE
(    
  position int IDENTITY PRIMARY KEY,
  value varchar(8000)   
)
AS

BEGIN
DECLARE @index int 

SET @text = LTRIM(RTRIM(@text))

SET @index = -1 
WHILE (LEN(@text) > 0) 
 BEGIN  
    SET @index = CHARINDEX(@delimiter , @text)  
    IF (@index = 0) AND (LEN(@text) > 0)  
      BEGIN   
        INSERT INTO @Strings VALUES (@text)
          BREAK  
      END  
    IF (@index > 1)  
      BEGIN   
        INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))   
        SET @text = RIGHT(@text, (LEN(@text) - @index))  
      END  
    ELSE 
      SET @text = RIGHT(@text, (LEN(@text) - @index)) 
    END
  RETURN
END

希望这有帮助!

Use the below function in its place.

CREATE  FUNCTION [dbo].[fn_Split](@text VARCHAR(8000), @delimiter VARCHAR(20) = ' ')
RETURNS @Strings TABLE
(    
  position int IDENTITY PRIMARY KEY,
  value varchar(8000)   
)
AS

BEGIN
DECLARE @index int 

SET @text = LTRIM(RTRIM(@text))

SET @index = -1 
WHILE (LEN(@text) > 0) 
 BEGIN  
    SET @index = CHARINDEX(@delimiter , @text)  
    IF (@index = 0) AND (LEN(@text) > 0)  
      BEGIN   
        INSERT INTO @Strings VALUES (@text)
          BREAK  
      END  
    IF (@index > 1)  
      BEGIN   
        INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))   
        SET @text = RIGHT(@text, (LEN(@text) - @index))  
      END  
    ELSE 
      SET @text = RIGHT(@text, (LEN(@text) - @index)) 
    END
  RETURN
END

Hope this helps!!

无妨# 2025-01-08 08:47:54

CTE - IE“WITH”是从 sql 2005 开始的一项功能。2000 年不可用...

CTE's - IE "WITH" are a feature starting in sql 2005. Not available in 2000...

花海 2025-01-08 08:47:54

您不能使用 CTE(通用表表达式 - WITH) 在 SQL Server 2000 中。它们是在 SQL Server 2005 中引入的。

You cannot use CTE's (Common Table Expressions - WITH) in SQL Server 2000. They were introduced in SQL Server 2005.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文