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