USE [TEST]
GO
CREATE PROCEDURE [dbo].[SQLDBA_DatabaseSpaceReport]
AS
BEGIN
DECLARE @dMaxLogDate Datetime = (SELECT TOP 1 LogDate FROM
SQLDBA_PS_Get_DbaDatabaseSpace ORDER BY LogDate DESC)
DECLARE
@html NVARCHAR(MAX),
@table VARCHAR(MAX)
SET @html = --HTML layout--
'<html><head>' +
'<H1 style="color: #000000">Prod Databases with less than 10%
space available</H1>' +
'<style>' +
'td {border: solid black 1px;padding-left:5px;padding-
right:5px;padding-top:1px;padding-bottom:1px;font-
size:9pt;color:Black;} ' +
'</style>' +
'</head>' +
'<body><table cellpadding=0 cellspacing=0 border=0>' +
'<tr bgcolor=33ffff>'+
'<td align=center>SqlInstance</b></td>' +
'<td align=center>Database</b></td>' +
'<td align=center>File Name</b></td>' +
'<td align=center>FileType</b></td>' +
'<td align=center>Used Space MB</b></td>' +
'<td align=center>Free Space MB</b></td>' +
'<td align=center>File Size MB</b></td>' +
'<td align=center>Percent Used</b></td>' +
'<td align=center>Auto Growth</b></td>' +
'<td align=center>Auto Grow Type</b></td></tr>';
SELECT @table = CONVERT(nvarchar(max) ,
(SELECT
td = [SqlInstance],
'',
td = [Database],
'',
td = [FileName],
'',
td = [FileType],
'',
td = CAST([UsedSpaceMB]as varchar(30)),
'',
td = CAST([FreeSpaceMB]as varchar(30)),
'',
td = CAST([FileSizeMB]as varchar(30)),
'',
td = CAST([PercentUsed]as varchar(30)),
'',
td = CAST([AutoGrowth]as varchar(30)),
'',
td = [AutoGrowType]
FROM [TEST].[dbo].SQLDBA_PS_Get_DbaDatabaseSpace D
INNER JOIN [TEST].[dbo].[Environment] E on D.ComputerName =
E.VirtualName AND E.ServerUse IN ('PROD','DR')
WHERE ( FileName NOT LIKE '%temp%' and FileName NOT LIKE 'SP%' and FileName
NOT LIKE 'Share%' )
AND LogDate = @dMaxLogDate and AutoGrowth > 0
ORDER BY [PercentUsed] desc
FOR XML PATH(N'tr'), TYPE));
SET @html = @html + @table + CHAR(10) +
N'</table></body></html>';
IF LEN(@HTML) > 10
BEGIN
--print @HTML
EXEC msdb.dbo.sp_send_dbmail
@recipients = '[email protected]',
@body_format = 'HTML',
@body= @HTML,
@subject = 'Alert!! Prod Databases With Less Than 10% Space Available!!',
@profile_name = 'SQL_DBA'
END
END
像这样的东西:(不是 100% 确定下面的代码或在哪里添加它)
CASE WHEN PercentUsed > 95 THEN
'<td align="Center"><font face="Arial" size="1" color="#FF0000"><b>' +
CAST([PercentUsed]as varchar(30) +'</font></td>'
ELSE
'<td align="Center"><font face="Arial" size="1" color="#40C211"><b>' +
CAST([PercentUsed]as varchar(30) +'</font></td>'
END +
'</tr>'
FROM [TEST].[dbo].SQLDBA_PS_Get_DbaDatabaseSpace
在这种情况下替换
td = CAST([PercentUsed]as varchar(30)),
为类似的东西:可能是你要找的。或者,如果您需要使用字体标签而不是更现代的样式选项(可能是因为您的某些收件人拥有真正古老的电子邮件阅读器),请根据需要进行编辑。
您的内部查询正在使用
FOR XML
hack *将所有内容连接成一个字符串,列名形成标签(在本例中它们都是td
),PATH
参数表示每一行都包含在<tr>
/中</tr>
。有关以这种方式形成 XML 文档片段(或在本例中为 HTML 文档片段)的更多信息,请参阅官方文档,例如https://learn.microsoft.com/en-us/sql/relational-databases/xml/for- xml-sql-server以及有关使用它进行字符串构建的各种教程,更普遍地搜索“TSQL FOR XML concatenation”或类似内容。* 我称之为 hack,因为您实际上并没有返回 XML 类型的结果,这是该功能的设计目的,它在这里的使用方式使其表现得像字符串连接聚合,最常见的用途是
FOR XML
in根据我的经验,请参阅https://sqlblog.org/2011/03/08/t-sql-tuesday-16-this-is-not-the-aggregate-youre-looking-for之类的文章,了解有关此技术和其他技术的注释达到相同的结果。