0

我一直在使用下面的这段代码来拆分输入 id,例如 FUNCTION_split('1,2,3',',')。现在的问题是它不适用于列表中超过 100 个的案例。有没有办法修改这段代码来处理超过 100 个 id?

CREATE FUNCTION [dbo].[FUNCTION_split]  
 (@list  NVARCHAR(MAX),  
  @delimiter NCHAR(1) = ',')   
    RETURNS TABLE   
AS  

RETURN  
    WITH cte_list([BeginChar], [EndChar]) AS (  
        SELECT [BeginChar] = CONVERT(BIGINT, 1), [EndChar] = CHARINDEX(@delimiter, @list + @delimiter)  
    UNION ALL  
        SELECT [BeginChar] = [EndChar] + 1, [EndChar] = CHARINDEX(@delimiter, @list + @delimiter, [EndChar] + 1)  
    FROM cte_list 
        WHERE  [EndChar] > 0  
    )  
    SELECT LTRIM(RTRIM(SUBSTRING(@list, [BeginChar],  
        CASE WHEN [EndChar] > 0 THEN [EndChar] - [BeginChar] ELSE 0 END))) AS [ParsedValue]  
    FROM cte_list 
        WHERE [EndChar] > 0 ; 
4

1 回答 1

1

您可以在查询中使用选项 MAXRECURSION

SELECT * FROM [dbo].[FUNCTION_split] ( @p1 , @p2 ) OPTION ( MAXRECURSION 30000 );

无限递归使用 Maxrecursion (0)

查询提示

于 2013-03-26T06:44:33.830 回答