• I too have faced this problem. The easiest method I have found is to set the string variable to an xml variable. You can select the variable and use for xml path clause to dynamical convert the string into "valid" xml text. You can then select the xml coumn and click the link to launch the code in a new window.

    SET NOCOUNT ON;

    GO

    DECLARE @t TABLE(

    ID INT,

    Str1 VARCHAR(1500),

    Str2 VARCHAR(1500),

    Str3 VARCHAR(1500),

    Str4 VARCHAR(1500)

    )

    INSERT INTO @t

    VALUES

    (

    1,

    REPLICATE('A',1500),

    REPLICATE('B',1500),

    REPLICATE('C',1500),

    REPLICATE('D',1500)

    );

    DECLARE @sql VARCHAR(MAX),

    @x XML,

    @Id INT

    SET @Id = 1

    SET @sql =

    (SELECT

    'SELECT ' + CHAR(13)

    + '''' + Str1 +'''' + ',' + CHAR(13)

    + '''' + Str2 +'''' + ',' + CHAR(13)

    + '''' + Str3 +'''' + ',' + CHAR(13)

    + '''' + Str4 +'''' + CHAR(13)

    + 'FROM t' + CHAR(13)

    + '--WHERE something = something'

    FROM @t

    WHERE id = 1)

    SET @x = (SELECT @sql AS [text()] FOR XML PATH(''))

    SELECT dataLength(@sql) AS SQL_Length

    SELECT @x