I want to report a bug and workaround in SQL Server 2017. Microsoft suggested to open a post here, so... The issue has to do with string concatenation in combination with CAST and REPLACE. Something which occurs in my code quite often. Run this query:
USE master;
PRINT @@Version
DECLARE @l NVARCHAR(MAX) = '';
SELECT @l += CAST(s.object_id AS NVARCHAR(50)) + ' ' + REPLACE(s.name, 'x', 'y') + ', '
FROM sys.tables s
ORDER BY s.name;
PRINT '> ' + @l;
SET @l = '';
SELECT @l = @l + CAST(s.object_id AS NVARCHAR(50)) + ' ' + REPLACE(s.name, 'x', 'y') + ', '
FROM sys.tables s
ORDER BY s.name;
PRINT '> ' + @l;
On my servers the output is:
Microsoft SQL Server 2017 (RTM-CU6) (KB4101464) - 14.0.3025.34 (X64)
Apr 9 2018 18:00:41
Copyright (C) 2017 Microsoft Corporation
Web Edition (64-bit) on Windows Server 2016 Standard 10.0 <X64> (Build 14393: ) (Hypervisor)
> 1787153412 MSreplication_options, 117575457 spt_fallback_db, 133575514 spt_fallback_dev, 149575571 spt_fallback_usg, 1483152329 spt_monitor,
> 1483152329 spt_monitor,
So using @l += works, @l = @l + doesn't. When you leave out the CAST or the REPLACE the results are the same.