如何获取XML文件(存储在SQL DB中)大小?

时间:2019-11-21 09:51:34

标签: sql sql-server xml

在我的数据库中,数据以二进制格式存储,并且通过SQL查询,我在查询中使用CAST将其转换为XML。

现在,我想通过添加另一列来在SQL中获取XML文件。

注意:我不想将这些数据导出到文件位置,因为它具有大量记录。

使用的查询:

select 
  AR.Column1, 
  AR.Column2, 
  CAST (Message as XML) AS "XML FILE"
from 
  [dbo].[Table1] AP 
  INNER JOIN [DBName].[dbo].[Table2] AR ON AR.Column1=AP.Column1 
where 
  (AR.InsertedDate BETWEEN '2019-11-19 00:00:00.000' AND '2019-11-19 23:59:59.477') 
  and AR.Column2='Delta' order by InsertedDate desc

1 个答案:

答案 0 :(得分:1)

DATALENGTH与可存储可变长度数据的数据类型结合使用时非常有用,例如:

  

图片
  ntext
  nvarchar
  文字
  varbinary
  varchar

SELECT AR.Column1, AR.Column2, 
       CAST (Message as XML) AS "XML FILE",
       CASE WHEN DATALENGTH(Message) <= 1024 THEN CONVERT(NVARCHAR, DATALENGTH(Message)) + ' BYTES'
            WHEN DATALENGTH(Message) >1024 AND DATALENGTH(Message) <= 1048576 THEN CONVERT(NVARCHAR, DATALENGTH(Message)/1024) + ' KB'
            WHEN DATALENGTH(Message) >1048576 AND DATALENGTH(Message) <= 1073741824 THEN   CONVERT(NVARCHAR, DATALENGTH(Message)/1048576) + ' MB'
            WHEN DATALENGTH(Message) >1073741824 AND DATALENGTH(Message) <= 109951162777  THEN   CONVERT(NVARCHAR, DATALENGTH(Message)/1073741824) + ' GB'
            ELSE CONVERT(NVARCHAR, DATALENGTH(Message)/109951162777) + ' TB' END AS "XML FILE SIZE"
FROM  [dbo].[Table1] AP 
INNER JOIN [DBName].[dbo].[Table2] AR ON AR.Column1=AP.Column1 
WHERE  (AR.InsertedDate BETWEEN '2019-11-19 00:00:00.000' AND '2019-11-19 23:59:59.477') 
       AND AR.Column2='Delta' 
ORDER BY InsertedDate DESC