if object_id( 'SQLTableToHTMLTable' ) is null
exec ( 'create proc SQLTableToHTMLTable as return null ' )
go
alter proc SQLTableToHTMLTable
( @pTableName sysname, @pOrderBy nvarchar(4000), @pHTML varchar(max) OUTPUT )
as
begin
-- Grab the header row html and the sql for selecting the data rows
declare @header varchar(max), @data varchar(max)
select
@header = '~END~', @data = '~END~'
select
@header = name + '</th><th>' + @header,
@data = '
isnull(convert(varchar(max),' + name + case when c.user_type_id in ( 58, 61 ) then ',121' else '' end + '),'' '') + ''</td><td>'' + ' + @data
from (
select * from tempdb.sys.columns where object_id = object_id( @pTableName )
union all
select * from sys.columns where object_id = object_id( @pTableName )
) c
order by column_id desc
select
@header = '<tr><th>' + replace( @header, '</th><th>~END~', '' ) + '</th></tr>',
@data = replace( @data, ' + ''</td><td>'' + ~END~', '' )
-- Grab the data row html
declare @sql nvarchar(4000)
select @sql = '
set @htmlOUT
= convert(
varchar(max),
(
select
td = ' + @data + '
from ' + @pTableName + isnull( '
order by ' + @pOrderBy, '' ) + '
for xml path( ''tr'' ), type
)
)
'
exec sp_executesql @sql, N'@htmlOUT varchar(max) OUTPUT', @htmlOUT = @pHTML OUTPUT
-- Place header and data rows in the table
set @pHTML
= '
<table cellpadding="2" cellspacing="2" border="1">
' + @header + '
' + replace( replace( replace( @pHTML, '<', '<' ), '>', '>' ), '</tr><tr>', '</tr>
<tr>' ) + '
</table>
'
end
go
---------------------------------
-- Example: Generate html table from a temp table
-- Step 1: Make sure the temp table doesn't exist yet
if object_id( 'tempdb..#temp' ) is not null
drop table #temp
-- Step 2: Create temp table
select
dbtable = object_name( object_id ),
*
into #temp
from sys.columns c
-- Step 3: Generate an html table from the temp table
declare @html varchar(max)
exec dbo.SQLTableToHTMLTable @pTableName = 'tempdb..#temp', @pOrderBy = 'object_id, column_id', @pHTML = @html output
-- Step 4: Render results as xml so we can view all the html
select html = convert(xml,@html)
go
---------------------------------
-- Example: Generate html table from a regular table
-- Step 1: Make sure the regular table doesn't exist yet
if object_id( 'TestTable' ) is not null
drop table TestTable
-- Step 2: Create regular table
select
dbtable = object_name( object_id ),
*
into TestTable
from sys.columns c
-- Step 3: Generate an html table from the regular table
declare @html varchar(max)
exec dbo.SQLTableToHTMLTable @pTableName = 'TestTable', @pOrderBy = 'object_id, column_id', @pHTML = @html output
-- Step 4: Render results as xml so we can view all the html
select html = convert(xml,@html)
go
declare @body varchar(max)
-- Create the body
set @body
= convert(
varchar(max),
(
select
td = dbtable
+ '</td><td>'
+ cast( entities as varchar(30) )
+ '</td><td>'
+ cast( rows as varchar(30) )
from (
select
dbtable = object_name( object_id ),
entities = count( distinct name ),
rows = count( * )
from sys.columns
group by object_name( object_id )
) as d
for xml path( 'tr' ), type
)
)
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( replace( @body, '<', '<' ), '>', '>' ), '</tr><tr>', '</tr>
<tr>' ) + '
</table>
'
print @body
select body = convert( xml, @body )
Sharing tips and tricks so that you might avoid the beatings that I've endured.
Categories
- Android (2)
- Backpacking (5)
- BBQ (3)
- Bikes (3)
- Boy Scouts (6)
- Breakfast (2)
- Cars (1)
- Chinese (2)
- CMD (8)
- eBooks (1)
- ELL (1)
- Family (13)
- Favorite Poetry (6)
- Flame (1)
- Fun (29)
- Instructables (4)
- Joannie (38)
- LDS Church (12)
- Men's Health Lists (2)
- Oracle (2)
- Pixar (1)
- PowerShell (4)
- Productivity (1)
- Projects (8)
- Recipe (46)
- Reign (2)
- RV (1)
- Scouting (7)
- Security (1)
- Shopping (7)
- Smoking (3)
- Softball (7)
- Sports (6)
- SQL Server (61)
- Stealth (1)
- Tools (58)
- Video (23)
- Visual Studio (5)
- Webelos (2)
Recent Posts
Archives
-
►
2014
(2)
- ► 04/20 - 04/27 (1)
- ► 03/30 - 04/06 (1)
-
►
2013
(9)
- ► 11/24 - 12/01 (3)
- ► 10/06 - 10/13 (1)
- ► 09/08 - 09/15 (1)
- ► 08/18 - 08/25 (2)
- ► 06/09 - 06/16 (1)
- ► 04/28 - 05/05 (1)
-
►
2012
(12)
- ► 06/24 - 07/01 (1)
- ► 06/03 - 06/10 (3)
- ► 05/27 - 06/03 (1)
- ► 05/20 - 05/27 (2)
- ► 04/22 - 04/29 (1)
- ► 03/18 - 03/25 (3)
- ► 03/04 - 03/11 (1)
-
►
2011
(84)
- ► 12/11 - 12/18 (1)
- ► 10/30 - 11/06 (2)
- ► 10/16 - 10/23 (1)
- ► 10/09 - 10/16 (1)
- ► 09/04 - 09/11 (1)
- ► 08/28 - 09/04 (1)
- ► 07/10 - 07/17 (4)
- ► 07/03 - 07/10 (2)
- ► 06/26 - 07/03 (6)
- ► 06/12 - 06/19 (2)
- ► 05/29 - 06/05 (1)
- ► 04/17 - 04/24 (1)
- ► 04/10 - 04/17 (2)
- ► 04/03 - 04/10 (2)
- ► 03/13 - 03/20 (1)
- ► 03/06 - 03/13 (1)
- ► 02/27 - 03/06 (1)
- ► 02/20 - 02/27 (1)
- ► 02/13 - 02/20 (5)
- ► 02/06 - 02/13 (1)
- ► 01/30 - 02/06 (41)
- ► 01/23 - 01/30 (4)
- ► 01/09 - 01/16 (1)
- ► 01/02 - 01/09 (1)
-
▼
2010
(56)
- ► 12/26 - 01/02 (1)
- ► 12/12 - 12/19 (4)
- ► 12/05 - 12/12 (2)
- ► 11/28 - 12/05 (1)
- ► 11/14 - 11/21 (6)
- ► 10/31 - 11/07 (4)
- ► 10/24 - 10/31 (4)
- ► 10/17 - 10/24 (1)
- ► 10/10 - 10/17 (2)
- ► 10/03 - 10/10 (4)
- ► 09/26 - 10/03 (3)
- ► 09/19 - 09/26 (1)
- ► 09/12 - 09/19 (9)
- ► 08/08 - 08/15 (1)
- ► 06/20 - 06/27 (1)
- ► 04/04 - 04/11 (1)
- ► 03/14 - 03/21 (1)
- ► 02/21 - 02/28 (3)
- ▼ 02/07 - 02/14 (3)
- ► 01/31 - 02/07 (2)
- ► 01/03 - 01/10 (2)
-
►
2009
(58)
- ► 12/20 - 12/27 (1)
- ► 09/20 - 09/27 (7)
- ► 09/06 - 09/13 (1)
- ► 07/26 - 08/02 (1)
- ► 07/12 - 07/19 (1)
- ► 06/14 - 06/21 (1)
- ► 06/07 - 06/14 (4)
- ► 05/31 - 06/07 (2)
- ► 05/24 - 05/31 (4)
- ► 05/17 - 05/24 (5)
- ► 05/10 - 05/17 (9)
- ► 05/03 - 05/10 (12)
- ► 04/26 - 05/03 (10)
Favorite Links
Favorite Tunes
Pronounced
/
'ri?l'ta?m
,
'ril-
/
Show Spelled Pronunciation
[
ree
-
uh
l-
tahym
,
reel
-
]
Show IPA
–noun
Fishing
.
of or pertaining to the period in which a fisherman must leave to go fishing. |
–adjective
Computers
.
of or pertaining to applications in which the computer must respond as rapidly as required by the user or necessitated by the process being controlled. |