纵有疾风起
人生不言弃

查询结果集转换成HTML存储过程

工作中经常需要用SQLServer发送报警或者业务报表邮件,每次现拼串也不是办法,故写了一个TableResult to HTML的存储过程

USE master;GO-- Description: Turns a table result into a formatted HTML table. Useful for emails. -- Any ORDER BY clause needs to be passed in the separate ORDER BY parameter.-- Author:zhangchuan  -- Date: 20180611-- ====================How to use=========================CREATE PROC [dbo].[sp_TableToHtml] (  @temptb nvarchar(MAX), --A temporary table to turn into HTML format. It should be begin with "#".  @orderBy nvarchar(MAX) = NULL, --An optional ORDER BY clause. It should contain the words 'ORDER BY'.  @html nvarchar(MAX) = NULL OUTPUT --The HTML output of the procedure.)ASBEGIN       SET NOCOUNT ON;    IF @orderBy IS NULL SET @orderBy = '' ELSE    SET @orderBy = REPLACE(@orderBy, '''', '''''');    DECLARE @ExecStr nvarchar(MAX)    SET @ExecStr='    SET @html =        N''<table border="1" style="font-size:12pt; font-family:verdana; text-align:right">'' +        N''<tr style="font-weight:bold">''    SET @html = @html + '    DECLARE @ColStr VARCHAR(MAX)    SELECT @ColStr=ISNULL(@ColStr+'<th>'+name+'</th>','<th>'+name+'</th>')    FROM  tempdb.sys.columns     WHERE object_id=(SELECT TOP 1 object_id FROM tempdb.sys.tables WHERE name like '%'+@temptb+'%' ORDER BY create_date DESC) ORDER BY column_id    SET @ExecStr=@ExecStr+'N'''+@ColStr+''''    SET @ExecStr=@ExecStr+'    SET @html = @html + N''</tr>''    '    DECLARE @RowStr VARCHAR(MAX)    SELECT @RowStr=ISNULL(@RowStr+'        ,td = LTRIM(ISNULL(['+name+'],0)),''''',',td = LTRIM(ISNULL(['+name+'],0)),''''')+CHAR(13)    FROM  tempdb.sys.columns     WHERE object_id=(SELECT TOP 1 object_id FROM tempdb.sys.tables WHERE name like '%'+@temptb+'%' ORDER BY create_date DESC) ORDER BY column_id        SET @ExecStr = @ExecStr+'        SET @html=@html+CAST(        (SELECT            '         + STUFF(@RowStr,1,1,'')        +'        FROM '+@temptb+' '+@orderBy+'        FOR XML PATH(''tr''), TYPE        ) AS NVARCHAR(MAX)     )    SET @html=replace(@html,''&lt;'',''<'')    SET @html=replace(@html,''&gt;'',''>'')    SET @html=@html+N''</table>''+CHAR(13)    '    --PRINT @ExecStr    EXEC sys.sp_executesql @ExecStr, N'@html nvarchar(MAX) OUTPUT', @html=@html OUTPUTENDGO

使用方法:

USE [master]GOIF OBJECT_ID('TEMPDB.DBO.#EndList') IS NOT NULL DROP TABLE #EndList--insert into temp tableSELECT TOP 10 *INTO #EndListFROM [DB_Monitor].[dbo].[T_dm_os_waiting_tasks]--Table to htmlDECLARE @html nvarchar(MAX);EXEC [sp_TableToHtml] @html = @html OUTPUT,  @temptb = N'#EndList', @orderBy = N'ORDER BY 1';--send the emailIF @html IS NOT NULL    EXEC msdb.dbo.sp_send_dbmail        @profile_name = 'DBA_Profile',        @recipients = 'zc_0101@163.com;',        --@blind_copy_recipients = 'zc_0101@163.com',        @subject = 'HTML email',        @body = @html,        @body_format = 'HTML'

效果图:

查询结果集转换成HTML存储过程插图

文章转载于:https://www.cnblogs.com/zc_0101/p/9167984.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » 查询结果集转换成HTML存储过程

分享到: 生成海报
avatar

评论 抢沙发

评论前必须登录!

立即登录   注册

切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活