Sql-Server

添加 CASE 語句以將低於 5% 的可用百分比標記為紅色 ELSE 綠色

  • January 24, 2018
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 = 'Tester@TEST.com',
@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)),為:

td = '<span style="color:' + CASE WHEN PercentUsed > 95 THEN '#FF0000' ELSE '#40C211' + '">'
  + CAST([PercentUsed]as varchar(30))
  + '</span>'
  ,

可能是您正在尋找的。或者,如果您需要使用字型標籤而不是更現代的樣式選項(可能是因為您的一些收件人擁有真正古老的電子郵件閱讀器),則根據需要進行編輯。

您的內部查詢正在使用FOR XMLhack *將所有內容連接成一個字元串,列名形成標籤(在這種情況下它們都是td),並且PATH參數意味著每行都包含在<tr>/中</tr>。有關以這種方式形成 XML 文件片段(或在本例中為 HTML 文件片段)的更多資訊,請參閱官方文件,例如https://docs.microsoft.com/en-us/sql/relational-databases/xml/for- xml-sql-server以及有關使用它進行字元串建構的各種教程,更一般地搜尋“TSQL FOR XML concatenation”或類似內容。

引用自:https://dba.stackexchange.com/questions/194618