T-SQL VARCHAR(MAX) Truncated

后端 未结 6 1731
谎友^
谎友^ 2020-12-19 01:32
DECLARE @str VARCHAR (MAX);

SELECT @str = COALESCE(@str + CHAR(10), \'\') +
       \'EXECUTE CreateDeno \' + CAST(ID AS VARCHAR) 
FROM   GL_To_Batch_Details
WHERE           


        
6条回答
  •  情书的邮戳
    2020-12-19 02:03

    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!)

提交回复
热议问题