The problem is the ambiguity of the + operator. When any argument is numeric, it assumes that you are doing a numeric addition, not a string concatenation.
If your source data is characters, you can fix them by completely removing the cast:
IF @TargetType = 'INT' BEGIN SELECT @SQLSTR = 'UPDATE ' + @TargetTable + ' SET ' + @TargetColumn + ' = ' + COALESCE(@FunctionValue, @Value) + ' ' END;
If your source data is numeric, you need to explicitly convert them to characters:
IF @TargetType = 'INT' BEGIN SELECT @SQLSTR = 'UPDATE ' + @TargetTable + ' SET ' + @TargetColumn + ' = ' + cast(cast(COALESCE(@FunctionValue, @Value) as int) as varchar(255)) + ' ' END;
I also moved "cast to int" outside coalesce() .
source share