SPlit功能错误 - 语句终止。在语句完成之前,最大递归100已用尽

时间:2013-03-26 06:33:42

标签: sql sql-server split

我一直在使用下面这段代码来分割输入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 ; 

1 个答案:

答案 0 :(得分:1)

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

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

无限递归使用Maxrecursion(0)

Query Hints