• I use a slightly different method to avoid some of the entitization problems FOR XML brings with it. Technically it is an undocumented method that I know of but I have not had trouble with it since SQL 2000 and I know of some other techniques that leverage it without trouble too:

    DECLARE @SQLCmd NVARCHAR(MAX) = N'';

    -- instead of using FOR XML just append the conjured string from each row to the same variable

    SELECT @SQLCmd += 'EXEC sys.sp_rename ' + --

    '@objname = N''' + SCHEMA_NAME(tab.schema_id) + '.' + tab.name + ''', ' + --

    '@newname = N''' + tab.name + '_' + CONVERT(VARCHAR(10), GETDATE(), 112) + ''', ' + --

    '@objtype = N''OBJECT'';' + -- OBJECT

    NCHAR(13) + NCHAR(10)

    FROM sys.tables tab;

    -- show the entire variable contents as an XML document...no truncation like with PRINT

    SELECT @SQLCmd AS [processing-instruction(query)]

    FOR XML PATH(''),

    TYPE;

    -- when ready, uncomment

    --EXEC sys.sp_executesql @SQLCmd;

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato