Just ran into a major headache when concatenating several @varchar(max)
variables together to build an email based on several different queries.
For efficiencies sake, I was using several varchars
to build the email at once, rather than going through roughly the same query two or three or more times to build it using only one varchar
.
This worked, right up until my varchars
got to longer than 8000 characters. Then the concatenation of them all into one varchar
(which I could shove into the @body
parameter of msdb.dbo.sp_send_dbmail
) returned "", and even LEN() wouldn't actually give me a length.
Anyhow, I've gotten around this by doing roughly the same queries several times and building the email with only one varchar(max)
.
TL;DR
I'm not happy with the solution. How could I have appended these varchar(max)
variables to each other?