I am trying to coalesce a large number of record Ids into a single, comma-separated string variable. However, the resulting n/varchar variable is always truncated at 4096 characters.
Here is the SQL statement I'm attempting -
DECLARE @docIds varchar(max)
SELECT @docIds = COALESCE(@docIds + ',', '') + CAST(docUid AS varchar(32))
FROM Documents
I have found a number of other SO links regarding n/varchar(max) truncation and concatenation, but cannot seem to resolve my query above. I've also tried -
DECLARE @docIds varchar(max) SET @docIds = ''
SELECT @docIds = @docIds + ',' + CAST(docUid AS varchar(32))
FROM Documents
SET @docIds = STUFF(@docIds, 1, 1, '');
From both queries above, SELECT LEN(@docIds)
will only ever return 4096 max.
QUESTION: How can I accomplish the above retrieval into a single string variable without truncation issues?