我一直在使用下面这段代码来分割输入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 ;
答案 0 :(得分:1)
您可以在查询中使用选项MAXRECURSION
SELECT * FROM [dbo].[FUNCTION_split] ( @p1 , @p2 ) OPTION ( MAXRECURSION 30000 );
无限递归使用Maxrecursion(0)