I am working with a query that will produce a html page. In the beginning that page is just a simple HTML table with tags. The tags will later be processed and replaced with subqueries (mostly scalar functions). This way combined HTML with replaced tags will be executed with sp_executesql to produce final html.
Excerpt of that HTML:
...Arial, Helvetica, Verdana, sans-serif; font-weight: bold;">Firma:</td><td style="font-size: 14px; font-family: Arial, Helvetica, Verdana, sans-serif;">'+(select cast(coalesce(Companyname,'') as nvarchar(max)) as result from Customer WHERE CustomerID=988082)+'</td></tr><tr><td style="font-size: 14px; font-family: Arial, Helvetica, Verdana, sans-serif; font-weight: bold;">Anrede:</td><td style="font-size: 14px; font-family: Arial, Helvetica, Verdana, sans-serif;">'+(select cast(coalesce(Anrede,'') as nvarchar(max)) as result from Customer WHERE CustomerID=988082)+'</td></tr><tr><td style="font-size: 14px; font-family: Arial, Helvetica, Verdana, sans-serif; font-weight: bold;">Vorname:</td><td style="font-size: 14px; font-family: Arial, Helvetica, Verdana, sans-serif;">'+(select cast(coalesce(Firstname,'') as nvarchar(max)) as result from Customer WHERE CustomerID=988082)+'</td></tr><tr><td style="font-size: 14px; font-family: Arial, Helvetica, Verdana, sans-serif; font-weight: bold;">Nachname:</td><td style="font-size: 14px; font-family: Arial, Helvetica, Verdana, sans-serif;">'+(select cast(coalesce(Lastname,'') as nvarchar(max)) as result from Customer WHERE CustomerID=988082)+'</td></tr><tr><td style="font-size: 1...
Now, I know that if I use anything but nvarchar(max) my result will be truncated to 4000, so I do casting of all function results to nvarchar(max) and my results is more than that but still strangely cut of from inside.
So when I run an example script I get 9043 characters as the result (with len function), and the result is cut off (note: from inside, near the end but not at the end). Now I append one letter and result is 9044. But if I add that letter 100 characters before the end result is still 9043!?
What is wrong with this? Why I can;t build a long T-SQL query to be executed with the expected results?
Thanks
UPDATE 1
Because the original solution is too long to display it here I will try to display smaller version just for you to see how I did it and what is my final doing with it:
declare @BodyTXT nvarchar(max)
set @BodyTXT = N'select ''some string' + (select cast('result of some function' as nvarchar(max))) +
N' another string' + (select cast('result of antoher function' as nvarchar(max))) +
N' another string' + (select cast('result of antoher function' as nvarchar(max))) +
N' another string' + (select cast('result of antoher function' as nvarchar(max))) +
N' another string' + (select cast('result of antoher function' as nvarchar(max))) +
N' another string' + (select cast('result of antoher function' as nvarchar(max))) +
N' another string' + (select cast('result of antoher function' as nvarchar(max))) +
N' another AAA string' + (select cast('result of antoher function' as nvarchar(max))) + N''''
execute sp_executesql @BodyTXT
Let's say that this is LEN = 9043 characters long and now, notice AAA if I add characters there the final results stays to be LEN = 9043, but if I add some strings at the very beginning string size will change and increase for the number of strings I add there.
Result will be cut off this way:
some stringresult of some function another stringresult of antoher function
another stringresult of antoher function another stringresult of antoher
function another stringresult of antoher function another stringresult of antoher
function another stringresult of antoher function another ...ult of antoher function
Can you see those ... dots almost at the end of the result it is there where I am missing rest of string that should be in the output (approx 12000, and I am having 9043)
UPDATE 2
Because this all is part of automated emailing system and it was running live I had to find solution and what I have done is removing some style information from the result table. So after cutting 1/3 of html I finally got result as expected BUT because size of the final string depends on how many products customer purchases for certainly this issue will come again. And also I want to know what is the reason for this behavior and how to overcome it.