<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL Server HTML Format - MSSQL Query</title>
	<atom:link href="https://mssqlquery.com/tag/sql-server-html-format/feed" rel="self" type="application/rss+xml" />
	<link>https://mssqlquery.com</link>
	<description>MSSQL and TSQL Programming and TSQL Examples</description>
	<lastBuildDate>Mon, 03 Apr 2023 05:36:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.1</generator>

<image>
	<url>https://mssqlquery.com/wp-content/uploads/2023/06/cropped-mssql-query-icon-32x32.png</url>
	<title>SQL Server HTML Format - MSSQL Query</title>
	<link>https://mssqlquery.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Procedure to Output Tables in HTML Format in SQL Server</title>
		<link>https://mssqlquery.com/procedure-to-output-tables-in-html-format-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Mon, 03 Apr 2023 05:33:48 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Stored Procedures]]></category>
		<category><![CDATA[SQL HTML Format]]></category>
		<category><![CDATA[SQL Server HTML Format]]></category>
		<category><![CDATA[sql server procedure]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=1484</guid>

					<description><![CDATA[<p>Hello everyone, In this article, I will share information about the procedure for outputting tables in HTML format in SQL Server In SQL Server, in some cases you may want to output the tables in HTML format. You can easily do this by using the following procedure. CREATE PROCEDURE [dbo].[SqlTableToHtml]&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/procedure-to-output-tables-in-html-format-in-sql-server">Procedure to Output Tables in HTML Format in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello everyone,</p>
<p>In this article, I will share information about the procedure for outputting tables in HTML format in SQL Server</p>
<p>In SQL Server, in some cases you may want to output the tables in HTML format.</p>
<p>You can easily do this by using the following procedure.</p>
<pre class="line-numbers"><code class="language-sql">CREATE PROCEDURE [dbo].[SqlTableToHtml] (@TABLENAME NVARCHAR(500),
@OUTPUT NVARCHAR(MAX) OUTPUT,
@TBL_STYLE NVARCHAR(1024) = '',
@TD_STYLE NVARCHAR(1024) = '',
@HDR_STYLE NVARCHAR(1024) = '')
AS

	DECLARE @exec_str NVARCHAR(MAX)
	DECLARE @ParmDefinition NVARCHAR(500)

	SET @exec_str = N'
DECLARE @exec_str  NVARCHAR(MAX)
DECLARE @ParmDefinition NVARCHAR(500)

select CustColHTML_ID=0,* INTO #CustomTable2HTML FROM ' + @TABLENAME + ' 

DECLARE @COUNTER INT
SET @COUNTER=0
UPDATE #CustomTable2HTML SET @COUNTER = CustColHTML_ID=@COUNTER+1 

DECLARE @HTMLROWS NVARCHAR(MAX) DECLARE @FIELDS NVARCHAR(MAX) 
SET @HTMLROWS='''' DECLARE @ROW NVARCHAR(MAX) 

SET @FIELDS=''&lt;tr&gt;''
SELECT @FIELDS=COALESCE(@FIELDS, '' '','''')+''&lt;th ' + @HDR_STYLE + '&gt;'' + name + ''&lt;/th&gt;''
FROM tempdb.sys.Columns
WHERE object_id=object_id(''tempdb..#CustomTable2HTML'')
AND name not like ''CustColHTML_ID''
SET @FIELDS=@FIELDS + ''&lt;/tr&gt;''

DECLARE @ColumnName  NVARCHAR(500)
DECLARE @maxrows INT
DECLARE @rownum INT

--Find row count of our temporary table
SELECT @maxrows=count(*) FROM  #CustomTable2HTML

DECLARE col CURSOR FOR
SELECT name FROM tempdb.sys.Columns
WHERE object_id=object_id(''tempdb..#CustomTable2HTML'')
AND name not like ''CustColHTML_ID''
ORDER BY column_id ASC

SET @rowNum=0
SET @ParmDefinition=N''@ROWOUT NVARCHAR(MAX) OUTPUT,@rowNum_IN INT''

While @rowNum &lt; @maxrows
BEGIN
  SET @HTMLROWS=@HTMLROWS + ''&lt;tr&gt;''

  SET @rowNum=@rowNum +1
  OPEN col
  FETCH NEXT FROM col INTO @ColumnName
  WHILE @@FETCH_STATUS=0
    BEGIN
      SET @exec_str=''SELECT @ROWOUT=(select COALESCE(['' + @ColumnName + ''], '''''''') AS ['' + @ColumnName + ''] from #CustomTable2HTML where CustColHTML_ID=@rowNum_IN)''

	  EXEC	sp_executesql 
			@exec_str,
			@ParmDefinition,
			@ROWOUT=@ROW OUTPUT,
            @rowNum_IN=@rownum

      SET @HTMLROWS =@HTMLROWS +  ''&lt;td ' + @TD_STYLE + '&gt;'' + @ROW + ''&lt;/td&gt;''
      FETCH NEXT FROM col INTO @ColumnName
    END
  CLOSE col
  SET @HTMLROWS=@HTMLROWS + ''&lt;/tr&gt;''
END

SET @OUTPUT=''''
IF @maxrows&gt;0
SET @OUTPUT= ''&lt;table ' + @TBL_STYLE + '&gt;'' + @FIELDS + @HTMLROWS + ''&lt;/table&gt;''

DEALLOCATE col
'

	DECLARE @ParamDefinition NVARCHAR(MAX)
	SET @ParamDefinition = N'@OUTPUT NVARCHAR(MAX) OUTPUT'
	EXEC sp_executesql @exec_str
					  ,@ParamDefinition
					  ,@OUTPUT = @OUTPUT OUTPUT

	RETURN 1


--Use of the Procedure


DECLARE @html NVARCHAR(MAX)
EXEC SqlTableToHtml 'Products'
					,@html OUTPUT
					,''
					,'style="border-top:1px #CCCCCC solid;padding:7px"'
					,'style="padding:7px"'
SELECT
	@html</code></pre>
<p>Create and run the above procedure and you will see a result similar to the one below.</p>
<p><img decoding="async" fetchpriority="high" class="alignnone wp-image-1487 size-full" src="https://mssqlquery.com/wp-content/uploads/2023/04/procedure-to-output-tables-in-html-format-in-sql-server-1.jpg" alt="Procedure to Output Tables in HTML Format in SQL Server" width="700" height="416" srcset="https://mssqlquery.com/wp-content/uploads/2023/04/procedure-to-output-tables-in-html-format-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2023/04/procedure-to-output-tables-in-html-format-in-sql-server-1-300x178.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>I got the result by trying the above procedure on the Products table on the Northwind database. You can also try it on your own tables.</p>
<p>As you can see, we have printed the table in HTML format.</p>
<p>Good luck to everyone in their working life and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 41</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/procedure-to-output-tables-in-html-format-in-sql-server">Procedure to Output Tables in HTML Format in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
