I’m developing a SQL function to retrieve history information for orders, but I’m encountering a puzzling problem with string concatenation.
CREATE FUNCTION fnRetrieveOrderHistory
(
@OrderID int
)
RETURNS varchar(8000) AS
BEGIN
declare HistoryLog cursor for
select StatusID, FirstName + ' ' + LastName AS UserFullName,
DateModified, CAST(Remarks AS varchar(8000)) AS Remarks
from OrderLogs ol
left join Staff st ON ol.StaffID = st.StaffID
where OrderID = @OrderID ORDER BY OrderSequence
declare @output varchar(8000)
declare @StatusID char(2)
declare @UserFullName varchar(255)
declare @DateModified datetime
declare @Remarks varchar(8000)
set @output = ''
OPEN HistoryLog
FETCH NEXT FROM HistoryLog INTO @StatusID, @UserFullName, @DateModified, @Remarks
WHILE @@FETCH_STATUS = 0
BEGIN
set @output = @output + RTRIM(CAST(@DateModified AS varchar(30))) + ': ' + @StatusID + ' by ' + @UserFullName + CHAR(13) + CHAR(10)
IF @Remarks IS NOT NULL
BEGIN
set @output = @output + '---' + @Remarks + CHAR(13) + CHAR(10)
END
FETCH NEXT FROM HistoryLog INTO @StatusID, @UserFullName, @DateModified, @Remarks
END
CLOSE HistoryLog
DEALLOCATE HistoryLog
RETURN @output
END
What confuses me is that the function operates as expected when all records have NULL remarks. However, when at least one record has a real remark instead of NULL, the function returns NULL instead of the anticipated string.
I’ve ensured that I’m verifying for NULL before concatenating, so I’m unsure of the reason behind this issue. I need to work with varchar(8000) since text types can’t be used in functions.
What might be the cause of this inconsistency?