Works great! Thanks very much for sharing!
I made a couple modifications to the stored procedure for tables with large number of columns.
In the Declaration section, increase size from 4000 to Max:
DECLARE @cmd NVARCHAR (Max)
DECLARE @cmd_loc NVARCHAR (Max)
Then because Print @Cmd is limited to 4000 characters:
-- To see @Cmd when longer than 4,000 characters
Print '@Cmd exceeds 4,000 characters.'
Select @cmd as Cmd Into #tmpCmd
Select Cmd from #tmpCmd
Drop Table #tmpCmd
PRINT @cmd -- Max length for Print is 4,000 Characters
--EXEC sp_executesql @cmd
Joe Settles, Nashville, TN