I think inside exec , NULL converted to an empty string. Here is a simpler example
DECLARE @sql_select2 varchar(200) ; exec('select 1' + @sql_select2)
returns 1 , although it is associated with a NULL value.
But when we do concatenation outside of it, it works as expected.
DECLARE @sql_select NVARCHAR(150) = 'SELECT GetDate()'; DECLARE @sql_select2 NVARCHAR(150) = NULL; DECLARE @sql VARCHAR(500) SET @sql = @sql_select + @sql_select2 EXEC(@sql)
returns nothing
source share