JustinStolle's answer in a different way. A few notes:
print
statement may truncate the string to 4000 characters, but my test string for example was 9520 characters in length.[tr/th]
indicates hierarchy, e.g., <tr><th>...</th></tr>
.[@name]
adds fields as XML attributes.null
in between fields prevents that.declare @body nvarchar(max)
select @body = cast((
select N'2' [@cellpadding], N'2' [@cellspacing], N'1' [@border],
N'Database Table' [tr/th], null [tr/td],
N'Entity Count' [tr/th], null [tr/td],
N'Total Rows' [tr/th], null,
(select object_name( object_id ) [td], null,
count( distinct name ) [td], null,
count( * ) [td], null
from sys.columns
group by object_name( object_id )
for xml path('tr'), type)
for xml path('table'), type
) as nvarchar(max))
print @body -- only shows up to 4000 characters depending