NVARCHAR在电子邮件程序中删除小数点

时间:2018-06-12 10:39:19

标签: sql sql-server stored-procedures casting

我正在忙于修改一个脚本,该脚本将临时表中的数据输出到发送给用户的脚本。它正在监视我们的SQL使用情况统计信息并用于报告等。我在下面的电子邮件代码之前输出了临时表,它正确地将我的十进制列保持为“12,12345”或DECIMAL(18, 5)

然而,发送电子邮件的最后一节(附在下面)继续删除我的小数点,而是输出为“12,000”。有没有人可能会得到一些帮助,我在剧本中尝试了各种演员,用“。”代替“,”。反之亦然。

SET @xml = CAST((SELECT  
                     LEFT(UPPER([Server]), 20) AS 'td', '',
                     LEFT([Database], 30) AS 'td', '',
                     LEFT(Device, 30) AS 'td', '',
                     LEFT([GB_Growth], 20) AS 'td', '',
                     REPLACE(CAST([GB_Current] AS DECIMAL(18, 5)), '.', ',') AS 'td', '',
                     REPLACE(CAST([GB_Previous] AS DECIMAL(18, 5)), '.', ',')        AS 'td', '',
                     ISNULL(CONVERT(CHAR(20),[Current_Date], 20), '--------------') AS 'td', '',
                     ISNULL(CONVERT(CHAR(20),[Previous_Date], 20), '--------------') AS 'td', ''
                 FROM    
                     [tempdb].[dbo].[DB_Dev_Sizediff] 
                 ORDER BY  
                     [Server], [Database]
                 FOR XML PATH('tr'), ELEMENTS) AS NVARCHAR(MAX))

我已经离开了我在上面脚本中的尝试(这不起作用),但我希望这适用于以下列:

GB_Growth, GB_Current and GB_Previous

脚本中的原始功能正在执行以下操作,但是我一直在尝试各种不同的事情,因此上面的原因有所不同:

ISNULL(CONVERT(CHAR(23), [GB_Current]), '-----') AS 'td', ''

此外,导致这一点的所有内容都在DECIMAL中,我已经确认了这一点,直到这一步。

输出屏幕截图

enter image description here

完整脚本:

--Temp Table
IF OBJECT_ID (N'tempdb.dbo.DB_Dev_Sizediff', N'U') IS NULL
  CREATE TABLE [tempdb].[dbo].[DB_Dev_Sizediff] (
      [Server]        [sysname]
    , [Database]      [sysname]
    , [Device]        [sysname]
    , [GB_Current]    [decimal](18,5)
    , [GB_Previous]   [decimal](18,5)
    , [GB_Growth]     [decimal](18,5)
    , [Current_Date]  [datetime]
    , [Previous_Date] [datetime]  
  )
ELSE TRUNCATE TABLE tempdb.dbo.DB_Dev_Sizediff ; 

-- Temp Table 2
IF OBJECT_ID (N'dbo.dbspace', N'U') IS NULL
  CREATE TABLE [dbo].[dbspace] (
      [runid]      [int]
    , [date]       [smalldatetime]
    , [servername] [sysname]
    , [dbname]     [varchar](128)
    , [devname]    [sysname]
    , [type]       [char](10)
    , [gb_alloc]   [decimal](18,5)
    , [gb_used]    [decimal](18,5)
  ) ; 


-- Get Latest Run From Run ID
WITH Current_Run AS
(
  SELECT   d.runid
         , d.servername
         , d.dbname
         , d.devname
         , d.gb_alloc
         , d.gb_used
         , d.[date]
  FROM   dbo.dbspace d
  WHERE  d.runid = (SELECT MAX(runid) FROM   dbo.dbspace)
) ,

Previous_Run AS
(
  SELECT   d.runid
         , d.servername
         , d.dbname
         , d.devname
         , d.gb_alloc
         , d.gb_used
         , d.[date]
  FROM   dbo.dbspace d
  WHERE  d.runid = (SELECT MAX(runid) FROM   dbo.dbspace WHERE  runid < (SELECT MAX(runid) FROM dbo.dbspace))  
)

INSERT INTO tempdb.dbo.DB_Dev_Sizediff
SELECT  CASE WHEN cr.servername IS NOT NULL 
             THEN cr.servername 
             ELSE pr.servername 
         END AS 'Server'
      , CASE WHEN cr.dbname IS NOT NULL 
             THEN cr.dbname 
             ELSE pr.dbname 
         END AS 'Database'
      , CASE WHEN cr.devname IS NOT NULL 
             THEN cr.devname 
             ELSE pr.devname 
         END AS 'Device'
      , CAST(cr.gb_alloc as DECIMAL (18, 5))
      , CAST(pr.gb_alloc as DECIMAL (18, 5))
      , CASE WHEN cr.gb_alloc IS NOT NULL
             THEN cr.gb_alloc - ISNULL(pr.gb_alloc,0) 
             ELSE 0 - pr.gb_alloc 
        END  AS 'GB_Growth'
      , cr.[date]
      , pr.[date]
FROM    Current_Run cr
FULL OUTER JOIN Previous_Run pr
ON      cr.servername = pr.servername
AND     cr.dbname = pr.dbname
AND     cr.devname = pr.devname
WHERE   cr.gb_alloc = pr.gb_alloc 
OR      cr.gb_alloc IS NULL
OR      pr.gb_alloc IS NULL   

--If any changes, compile our email below
IF EXISTS (SELECT 1 FROM tempdb.dbo.DB_Dev_Sizediff)
BEGIN
  DECLARE   @subj   VARCHAR(200)
          , @body  NVARCHAR(MAX)
          , @xml   NVARCHAR(MAX)
  SELECT    @subj  = 'Database device size changes'
  SET       @body = '<html><body>
               <p><font size="2" face="monaco">                         
               Listed below are are all databases that have grown since our last scan. <BR><BR> 
               <H3>Database Device Size Changes in GB</H3>
               <table border="1" cellpadding="5"> 
               <p><font size="2" face="monaco">                  
               <tr>
               <th>Server</th> <th>Database</th> <th>Device</th> <th>GB Growth</th> <th>GB Current</th> <th>GB Previous</th> <th>Current Check</th> <th>Previous Check</th></tr>'
  SET   @xml =  CAST(
                (SELECT  LEFT(UPPER([Server]),20)                                      AS 'td',''
                       , LEFT([Database], 30)                                          AS 'td',''
                       , LEFT(Device, 30)                                              AS 'td',''
                       , [GB_Growth]                                           AS 'td',''
                       , [GB_Current]               AS 'td',''
                       , [GB_Previous]        AS 'td',''
                       , ISNULL(CONVERT(CHAR(20),[Current_Date],20),'--------------')  AS 'td',''
                       , ISNULL(CONVERT(CHAR(20),[Previous_Date],20),'--------------') AS 'td',''
                 FROM    [tempdb].[dbo].[DB_Dev_Sizediff] 
                 ORDER BY  [Server], [Database]
                 FOR XML PATH('tr'), ELEMENTS ) 
                 AS NVARCHAR(MAX))

--Compile Email and Send It using Defined Settings           
  SET @body = @body + @xml +'</table></body></html>'
  EXEC msdb.dbo.sp_send_dbmail 
       @profile_name =  @profile_name
     , @recipients   =  @recipients
     , @subject      =  @subj  
     , @body         =  @body
     , @body_format  = 'HTML'
END 

来源 - http://www.sqlservercentral.com/articles/Monitoring/118079/

1 个答案:

答案 0 :(得分:0)

我不确定后台发生了什么,但我决定修改脚本以永远不使用临时表并直接保存到物理表中。一旦这样做,我在最终的XML构建语句中修改了from子句,并且查询工作没有将任何逻辑应用于实际列。

感谢您的帮助和建议,我想我只需要停下来看一下就可以放松心情了。