我遇到了一个失败的表值函数的连接,我无法解释它。我在下面创建了一个测试脚本,我在 SQL Server 2008 R2 上运行它
谁能理解为什么在测试脚本结束时第一个 SELECT 语句有效而第二个语句无效?
CREATE TABLE dbo.Test (Setting INT NOT NULL, Value NVARCHAR(MAX) NULL)
GO
INSERT dbo.Test (Setting, Value)
SELECT 3151, '2~-10011;2~-10012'
GO
CREATE FUNCTION [dbo].[CTESplitChunk] (@list nvarchar(MAX),
@delim nchar(1) = ',')
RETURNS @t TABLE (str nvarchar(4000) NOT NULL) AS
BEGIN
DECLARE @slice nvarchar(4000),
@textpos int,
@maxlen int,
@stoppos int,
@lastone bit
SELECT @textpos = 1, @maxlen = 4000 - 2, @lastone = 0
WHILE @lastone = 0
BEGIN
IF datalength(@list) / 2 - (@textpos - 1) >= @maxlen
BEGIN
SELECT @slice = substring(@list, @textpos, @maxlen)
SELECT @stoppos = @maxlen -
charindex(@delim COLLATE Slovenian_BIN2, reverse(@slice))
SELECT @slice = left(@slice, @stoppos) + @delim
SELECT @textpos = @textpos - 1 + @stoppos + 2 -- On the other side of the comma.
END
ELSE
SELECT @slice = substring(@list, @textpos, @maxlen) + @delim,
@lastone = 1
;WITH csvtbl(start, stop) AS (
SELECT start = 1,
stop = charindex(@delim COLLATE Slovenian_BIN2, @slice)
UNION ALL
SELECT start = stop + 1,
stop = charindex(@delim COLLATE Slovenian_BIN2,
@slice, stop + 1)
FROM csvtbl
WHERE stop > 0
)
INSERT @t (str)
SELECT ltrim(rtrim(
substring(@slice, start,
CASE WHEN stop > 0 THEN stop - start ELSE 0 END)))
FROM csvtbl
WHERE stop > 0
OPTION (MAXRECURSION 0)
END
RETURN
END
GO
SELECT Value
FROM dbo.Test t
WHERE t.Setting = '3151'
GO
SELECT Value
FROM dbo.Test t
INNER JOIN dbo.CTESplitChunk (t.Value, ';') s ON 1 = 1
WHERE t.Setting = '3151'