SQL Server 2014 - Standard Edition (64-bit)
Version 12.0.4100.1
OS: Windows NT 6.3 (9600) --> Windows Server 2012 R2
I'm in the process of evaluating moving from SS 2008 to SS 2014 and found a problem.
I've created a small bit of code that replicates the problem.
Create the following function. Then change the name to my_splitter_2 and create the function again.
This just takes a CSV string and turns the values into a table.
CREATE FUNCTION dbo.my_splitter_1 ( @csv_list VARCHAR(MAX) ) RETURNS @ret TABLE (csv_item VARCHAR(100)) AS BEGIN DECLARE @delimiter_position INT DECLARE @working_csv_list VARCHAR(MAX) SET @delimiter_position = 0 SET @working_csv_list = @csv_list SET @delimiter_position = CHARINDEX(',', @working_csv_list) WHILE (@delimiter_position <> 0) BEGIN INSERT @ret (csv_item) SELECT LTRIM(RTRIM(LEFT(@working_csv_list, @delimiter_position - 1))) SET @working_csv_list = SUBSTRING ( @working_csv_list, @delimiter_position + 1, LEN(@working_csv_list) - @delimiter_position ) SET @delimiter_position = CHARINDEX(',', @working_csv_list) END if (@working_csv_list IS NOT NULL) BEGIN INSERT INTO @ret (csv_item) SELECT LTRIM(RTRIM(@working_csv_list)) END RETURN END
Now run the following
DECLARE @ListValue VARCHAR(MAX) DECLARE @value VARCHAR(MAX) SET @value = '2' SET @ListValue = '1,2,3,A' DECLARE @cnt INT -- Join my_splitter_1 to my_splitter_1 SELECT @cnt = ( SELECT COUNT(*) FROM dbo.my_splitter_1(@value) L1 INNER JOIN dbo.my_splitter_1(@ListValue) L2 ON L1.csv_item = L2.csv_item ) SELECT @cnt -- Join my_splitter_1 to my_splitter_2 SELECT @cnt = ( SELECT COUNT(*) FROM dbo.my_splitter_1(@value) L1 INNER JOIN dbo.my_splitter_2(@ListValue) L2 ON L1.csv_item = L2.csv_item ) SELECT @cnt -- Join my_splitter_2 to my_splitter_2 SELECT @cnt = ( SELECT COUNT(*) FROM dbo.my_splitter_2(@value) L1 INNER JOIN dbo.my_splitter_2(@ListValue) L2 ON L1.csv_item = L2.csv_item ) SELECT @cnt
The results are 4 then 1 then 4.
So when the same function is used in the same operation, the wrong result is returned.
Any thoughts on what is going on?
PLEASE don't suggest re-writing code.
I have 10's of thousands of lines of code and I can't go through them all by hand looking to find potential problems.