

DECLARE varchar(max)ĭECLARE NVARCHAR(MAX), NVARCHAR(MAX), NVARCHAR(MAX), NVARCHAR(MAX) Ofcourse you should create mail profile before run this.

I use this for running scripts on two tables/views with SQL job and send results as two HTML tables via mail. Rank Player Name Ranking Points Country 'ĮXEC = 'SQL ALERTING', - replace with your SQL Database Mail = - replace with your email = 'E-mail in Tabular Format' AS 'td','', Country AS 'td'įOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX)) SELECT 2,'Roger Federer',7965,'Switzerland' This might give you some idea - CREATE TABLE #Temp
TABLE FORMAT IN HTML CODE
Related: Here is similar code to turn any arbitrary query into a CSV string. SELECT = + '''', '''') + '''' + name + ''''ĮXEC sys.sp_executesql nvarchar(MAX) OUTPUT', OUTPUTĮXEC spQueryToHtmlTable = OUTPUT, = N'SELECT * FROM dbo.People', = N'ORDER BY FirstName' SET = ''SET = CAST(( SELECT '' + + '' FROM #dynSql ' + + ' FOR XML PATH(''''tr''''), ELEMENTS XSINIL) AS nvarchar(max))''ĮXEC sys.sp_executesql nvarchar(MAX) OUTPUT'', OUTPUT WHERE object_id = object_id(''tempdb.#dynSql'') SELECT * INTO #dynSql FROM (' + + ') sub It should contain the words 'ORDER nvarchar(MAX) = NULL OUTPUT -The HTML output of the procedure. It should not include an ORDER BY nvarchar(MAX) = NULL, -An optional ORDER BY clause. =ĬREATE PROC nvarchar(MAX), -A query to turn into HTML format. Any ORDER BY clause needs to be passed in the separate ORDER BY parameter. Description: Turns a query into a formatted HTML table. I made a dynamic proc which turns any random query into an HTML table, so you don't have to hardcode columns like in the other responses.
