Saturday 16 January 2016

Selection result as HTML in sql server


We can store the sql server in HTML using below sql query. Here I am using AdventureWorks2008R2 .


DECLARE @html nvarchar(max), @table nvarchar(max);
SET @html =
N'<html><head><title>Product List</title></head>' + CHAR(10) +
N'<body style="font-family: Arial">' +
N'<h2>Product List</h2>' +
'<table border="1">' +
N'<tr>' +
    N'<th width="120">ProductID</th>' +
    N'<th width="360">Name</th>' +
    N'<th width="90">ProductNumber</th>' +
   N'</tr>' + CHAR(10);
 SELECT @table =
       CONVERT(nvarchar(max),
              (SELECT td = ProductID
                      ,''
                      ,td = Name
                      ,''
                      ,td = ProductNumber
                      ,''                     
                FROM [AdventureWorks2008R2].[Production].[Product]
                FOR XML PATH(N'tr'), TYPE));
 SET @html = @html + @table + CHAR(10) +
            N'</table></body></html>'; 
-- Final selection to return result.
SELECT @html As [HTML Code];


We get the result in HTML code.

We can save the result in note pad and save it as HTML.

We can open it.  You can see it on browser.
  


Enjoy it.

Popular Posts