我有一个拆分字符串的函数(为清楚起见粘贴在末尾)。此功能在单独使用时按预期工作。例子:
SELECT value
FROM dbo.mg_fn_Split('2#1','#')
退货
-- value --
-- 2 --
-- 1 --
-----------
但是当在“WHERE IN”子句中使用时,如本例所示(稍后将详细介绍 tableA):
SELECT * FROM TableA WHERE TableA.id IN
(
SELECT value
FROM dbo.mg_fn_Split('2#1','#')
)
我收到错误:“传递给 LEFT 或 SUBSTRING 函数的长度参数无效。”
此处以 TableA 为例。使用不同的表(假设它们有 id 列)有时会返回正确的结果,而在其他表上我会得到错误。
我假设它与执行顺序有关,但我仍然看不到什么会“破坏”该功能。
我正在寻找“正在发生的事情”的解释,而不是“改用它”。我知道我可以使用连接来获取结果。
函数定义:
-- Description: Returns a table containing the results of a string-split operation.
-- Params:
-- DelimitedList: The string to split
-- Delimiter: The delimiter char, defaults to ','
-- Columns:
-- Position - The char index of the item
-- Value - The actual item
-- =============================================
CREATE Function [dbo].[mg_fn_Split]
(
@DelimitedList nvarchar(max)
, @Delimiter nvarchar(2) = ','
)
RETURNS TABLE
AS
RETURN
(
With CorrectedList As
(
Select Case When Left(@DelimitedList, Len(@Delimiter)) <> @Delimiter Then @Delimiter Else '' End
+ @DelimitedList
+ Case When Right(@DelimitedList, Len(@Delimiter)) <> @Delimiter Then @Delimiter Else '' End
As List
, Len(@Delimiter) As DelimiterLen
)
, Numbers As
(
Select TOP( Coalesce(DataLength(@DelimitedList)/2,0) ) Row_Number() Over ( Order By c1.object_id ) As Value
From sys.columns As c1
Cross Join sys.columns As c2
)
Select CharIndex(@Delimiter, CL.list, N.Value) + CL.DelimiterLen As Position
, Substring (
CL.List
, CharIndex(@Delimiter, CL.list, N.Value) + CL.DelimiterLen
, CharIndex(@Delimiter, CL.list, N.Value + 1)
- ( CharIndex(@Delimiter, CL.list, N.Value) + CL.DelimiterLen )
) As Value
From CorrectedList As CL
Cross Join Numbers As N
Where N.Value <= DataLength(CL.List) / 2
And Substring(CL.List, N.Value, CL.DelimiterLen) = @Delimiter
)
编辑:我设置了一个小提琴来展示这个: http ://sqlfiddle.com/#!3/9f9ff/3