DECLARE @str VARCHAR (MAX);
SELECT @str = COALESCE(@str + CHAR(10), \'\') +
\'EXECUTE CreateDeno \' + CAST(ID AS VARCHAR)
FROM GL_To_Batch_Details
WHERE
The PRINT command is certainly limited to 8000 chars, irrespective of the length of the output (or whether it is varchar(max)). To work around this you need to output the string in chunks of <8000 chars
Update: In answer to your edit, exec doesn't limit the string length. I've put together the following example to show this:
DECLARE @str VARCHAR (MAX);
;WITH CTE_Count AS
(
select counter = 1
union all
select counter = counter+1
from CTE_Count
Where counter < 2000
)
SELECT
@str=COALESCE(@str + CHAR (10) ,
'' ) + 'select value=' + CAST (counter AS VARCHAR)
from
CTE_Count
Option (MAXRECURSION 0)
PRINT len(@str);--SELECT @str;
exec (@str)
Running this prints the length as 34892 chars, and all 2000 execute statements do run (be warned, it may take a few mins!)