내 사용자는 "XML PATH"솔루션을 찾아서 사용하려고하지만 프로그래머가 아니기 때문에 혼란스러워합니다. XML PATH 사용 방법을 배우는 대신 테이블 이름을 사용하고 HTML 태그가 포함 된 문자열을 반환하여 테이블을 HTML 테이블로 표시하는 프로 시저를 만들었습니다. 내 솔루션은 데이터 소스에서 HTML 테이블의 열 이름을 동적으로 파생합니다.
프로 시저 HtmlTable의 샘플 사용
CREATE table ##foo (bar1 int, bar2 varchar(20), bar3 datetime)
INSERT into ##foo values (1, 'Abcdef', getdate())
INSERT into ##foo values (2, 'Ghijkl', '05/05/15')
DECLARE @tableHtml varchar(max)
EXEC dbo.HtmlTable
'##foo',
@tableHtml output
PRINT @tableHtml
@tableHtml 변수에는 전자 메일 본문에 포함하기에 적합한 HTML 테이블에 대한 태그 만 포함되어 있습니다. 단독으로 보려면 출력을 html, head 및 body 태그로 랩핑해야합니다.
프로 시저 HtmlTable의 출력
프로 시저 코드 테이블
/*
Author: Leigh Haynes
Date: February 2015
Notes: Takes a table name as string parameter and returns a string that contains HTML markup to display the table contents as an HTML table.
The input table should be sorted before invoking HtmlTable.
*/
CREATE PROCEDURE [dbo].[HtmlTable]
@data_source varchar (100) = NULL,
@tableHTML varchar(max) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE
@db varchar(50),
@table varchar(100),
@cmd varchar(400),
@rcd_cnt int,
@sql nvarchar(1000);
--use procedure DataSourceCheck to see if @data_source is valid
EXEC dbo.DataSourceCheck @data_source, @db output, @table output;
IF @db is NULL --if the data source is not good, @db comes back NULL, and @table holds info as to the problem (either the table does not exist, or it is empty).
BEGIN
SET @tableHtml = @table;
RETURN;
END;
--We have a good table. Use information_schema metadata for table to get column names.
IF OBJECT_ID ('tempdb..##columnNames') IS not null DROP TABLE ##columnNames;
CREATE table ##columnNames (column_name varchar(50), position int identity);
SET @sql = 'USE ' + @db + '; INSERT into ##columnNames SELECT column_name from information_schema.columns where table_name = ''' + @table + ''' order by ordinal_position';
EXEC master.sys.sp_executesql @sql;
--use ##columnNames to create table ##columnPivot with the proper number of fields to hold data
IF OBJECT_ID ('tempdb..##columnPivot') IS not null DROP TABLE ##columnPivot;
CREATE table ##columnPivot (f1 varchar(200));
DECLARE
@i int = 2,
@fieldct int,
@column varchar(50),
@field varchar(200),
@value varchar(100),
@html varchar(max) = '';
SET @fieldct = (SELECT COUNT(*) from ##columnNames);
WHILE @i <= @fieldct --loop through adding a field to ##columnPivot for each column. Max field len is 200.
BEGIN
SET @sql = 'ALTER table ##columnPivot ADD f' + cast (@i as varchar(2)) + ' varchar(200)';
EXEC master.sys.sp_executesql @sql;
SET @i = @i + 1;
END
--##columnPivot is constructed but empty. Columns are named f1, f2, f3, etc
--construct dynamic SQL string that will be executed to populate ##columnPivot
SET @sql = 'INSERT into ##columnPivot SELECT ';
SET @i = 1;
SET @fieldct = (SELECT count(*) from ##columnNames);
WHILE @i <= @fieldct - 1
BEGIN
SET @column = (SELECT top 1 column_name from ##columnNames where position = cast (@i as varchar(2)));
SET @field = 'CAST([' + @column + '] as varchar(200)),';
SET @sql = @sql + @field;
SET @i = @i + 1;
END
SET @column = (SELECT top 1 column_name from ##columnNames where position = @fieldct);
SET @field = 'CAST([' + @column + '] as varchar(200)) FROM ' + @data_source;
SET @sql = @sql + @field; --@sql now contains the SQL statement that will insert data from @data_source into ##columnPivot
--execute @sql to insert into ##columnPivot the data from @data_source table
EXEC master.sys.sp_executesql @sql;
--format the output
IF OBJECT_ID ('tempdb..#columns') IS not null DROP TABLE #columns;
--use a copy of ##columnNames, because next steps delete from this table, and ##columnNames data is needed below. Does not need to be a global temp.
SELECT *
into #columns
from ##columnNames
order by position;
SET @fieldct = (SELECT count(*) from #columns);
SET @i = 1;
--create the header row for the table containing column names from the @data_source
WHILE @i <= @fieldct
BEGIN
SET @field = (SELECT top 1 column_name from #columns order by position);
SET @html = @html + '<td bgcolor="#dedede"><b>' + @field + '</b></td>';
SET @i = @i + 1;
DELETE from #columns where column_name = @field;
END
SET @html = '<tr>' + @html + '</tr>'; --now @html contains the header row of the output table
--populate ##columnPivot by working through the data row by row.
ALTER table ##columnPivot add id_key int identity;
DECLARE
@j int = 1,
@fieldcnt int,
@cell varchar(100),
@row varchar(500) = '';
SET @i = 1;
SET @fieldcnt = (SELECT count(*) from ##columnNames);
SET @rcd_cnt = (SELECT count(*) from ##columnPivot);
WHILE @i <= @rcd_cnt --this loop executes one time for each row of data
BEGIN
SET @j = 1;
WHILE @j <= @fieldcnt --this loop executes one time for each column (cell) of data
BEGIN
SET @sql = 'SELECT @value = f' + cast (@j as varchar(2)) + ' from ##columnPivot where id_key = ' + cast (@i as varchar(2));
EXEC master.sys.sp_executesql @sql, N'@value varchar(200) OUTPUT', @value OUTPUT;
SET @cell = '<td>' + ISNULL (@value, '<br>') + '</td>'; --need to use <br> if the cell is empty
SET @row = @row + @cell;
SET @j = @j + 1;
END
SET @row = '<tr>' + @row + '</tr>';
SET @html = @html + @row;
SET @row = '';
DELETE from ##columnPivot where id_key = cast (@i as varchar(2));
SET @i = @i + 1;
END
SET @tableHTML = '<table border="1" cellspacing="0" cellpadding="5">' + @html + '</table><br>';
END
매개 변수 @data_source에 유효한 테이블 이름이 포함되어 있는지 확인하기 위해 "DataSourceCheck"프로 시저를 호출하고 있습니다. DataSourceCheck는 별도의 프로 시저 일 필요는 없습니다. SQL은 HtmlTable 프로 시저에 직접 포함될 수 있지만 모듈 성과 재사용 성을 위해 별도로 유지합니다.
프로 시저 DataSourceCheck의 샘플 사용
CREATE table ##foo (bar1 int, bar2 varchar(20), bar3 datetime)
INSERT into ##foo values (1, 'Abcdef', getdate())
INSERT into ##foo values (2, 'Ghijkl', '05/05/15')
DECLARE @table varchar(200), @db varchar(50)
EXEC dbo.DataSourceCheck
'##foo',
@db output,
@table output
PRINT @db
PRINT @Table
산출
tempdb
##foo
이제 테이블을 삭제하고 어떻게되는지 확인하십시오.
DROP table ##foo
DECLARE @table varchar(200), @db varchar(50)
EXEC dbo.DataSourceCheck
'##foo',
@db output,
@table output
PRINT @db
PRINT @Table
산출
<br>Table ##foo does not exist or is improperly qualified.<br>
프로 시저 코드 DataSourceCheck
/*
Author: Leigh Haynes
Date: February 2015
Notes: Called by HtmlTable and CreateCsvFile to check validity of data source that is going to turn into an HTML table or a CSV file.
*/
CREATE PROCEDURE [dbo].[DataSourceCheck]
@dataSource varchar (100) = NULL,
@db varchar(50) = NULL output,
@table varchar(100) = NULL output
AS
BEGIN
DECLARE
@buffer varchar(100),
@object varchar(100),
@objectId bigint,
@schema varchar(50),
@rcd_cnt int,
@tableHtml varchar(200),
@sql nvarchar(1000)
SET @buffer = @dataSource;
--cannot accesss a local temp table. Return.
IF SUBSTRING (@buffer, 1, 1) = '#' and SUBSTRING (@buffer, 2, 1) <> '#'
BEGIN
--use LEFT 25 to make sure the local temp table name isn't too long for the @table varchar(100) variable.
SET @table = '<br>Table ' + LEFT (@dataSource, 25) + ' is a local temp table. Must use a global temp or permanent table.<br>';
RETURN;
END;
--set up the object name in the right format so you can check the OBJECT_ID
ELSE IF (SUBSTRING (@buffer, 1, 2) = '##')
BEGIN
SET @db = 'tempdb';
SET @table = @dataSource;
SET @object = @db + '..' + @table; --need to include tempdb so OBJECT_ID finds the temp table
END;
ELSE
BEGIN
--deal with schema
SET @db = SUBSTRING (@buffer, 1, charindex ('.', @buffer) - 1);
SET @buffer = replace (@buffer, @db + '.', '');
IF SUBSTRING (@buffer, 1, 1) = '.'
BEGIN
SET @schema = '..';
SET @buffer = replace (@buffer, '.', '');
END
ELSE
BEGIN
SET @schema = SUBSTRING (@buffer, 1, charindex ('.', (@buffer)) - 1);
SET @buffer = replace (@buffer, @schema + '.', '');
END
SET @table = @buffer;
SET @object = @dataSource;
END;
--does our data source exist? Check the object_id. If object does not exist, return.
SET @objectId = OBJECT_ID (@object, 'U');
IF @objectId is NULL
BEGIN
SET @db = NULL;
SET @table = '<br>Table ' + @dataSource + ' does not exist or is improperly qualified.<br>';
RETURN;
END;
--we have a valid data source. Check that it has rows and notify if empty.
SET @sql = 'SELECT @rcd_cnt = count(*) from ' + @dataSource;
EXEC master.sys.sp_executesql @sql, N'@rcd_cnt int OUTPUT', @rcd_cnt OUTPUT;
IF @rcd_cnt = 0
BEGIN
SET @db = NULL;
SET @table = '<br>Table ' + @dataSource + ' is empty.<br>';
RETURN;
END;
END
HTML 테이블 대신 테이블에서 CSV 파일을 만드는 HtmlTable 버전도 있습니다.