Friday, October 24, 2008 9:13 AM
Format query output into an HTML table - the easy way
Ok, I wrote a big stored procedure last time my entry on "send table or view as embedded html"; I've found a much simpler method in pure SQL....
declare @body varchar(max)
-- Create the body
set @body = cast( (
select td = dbtable + '</td><td>' + cast( entities as varchar(30) ) + '</td><td>' + cast( rows as varchar(30) )
select dbtable = object_name( object_id ),
entities = count( distinct name ),
rows = count( * )
group by object_name( object_id )
) as d
for xml path( 'tr' ), type ) as varchar(max) )
set @body = '<table cellpadding="2" cellspacing="2" border="1">'
+ '<tr><th>Database Table</th><th>Entity Count</th><th>Total Rows</th></tr>'
+ replace( replace( @body, '<', '<' ), '>', '>' )
Then just use whatever email mechanism you want, for instance my CLR proc: http://sqlblogcasts.com/blogs/tonyrogerson/archive/2007/12/31/clr-stored-procedure-to-utilise-more-of-the-mailmessage-net-class-rather-than-db-mail.aspx
Filed under: SQL Server, SQL Development, SQL Server User Group