简体   繁体   English

SQL 中的 XML 数据类型列

[英]XML Data type column in SQL

I have a table called assessments.我有一个叫做评估的表。 In that table, we are storing responses to a list of questions from the application and we have an XML column with an XML data type that stores the same responses for the questions in the XML format.在该表中,我们存储了对来自应用程序的问题列表的响应,并且我们有一个 XML 列,其 XML 数据类型以 XML 格式存储问题的相同响应。 Mostly the XML column will have a null value when the user is refused to pass it.大多数情况下,当用户被拒绝传递时,XML 列将具有空值。 Can anyone know how much storage it will take for the XML column if the values are passed and not passed?.任何人都可以知道如果值被传递和未传递,XML 列将占用多少存储空间?

The internal representation of an XML document is not what you see when looking at the XML document. XML 文档的内部表示与您在查看 XML 文档时所看到的不同。

Here is a good link on the subject: Where to find the size of SQL Server data types?这是关于该主题的一个很好的链接: 在哪里可以找到 SQL Server 数据类型的大小?

For example, contrary to popular belief, XML is smaller than JSON.例如,与流行的看法相反,XML 比 JSON 小。

SQL SQL

DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, jsondata NVARCHAR(MAX), xmldata XML);
INSERT INTO @tbl (jsondata, xmldata)
VALUES
(N'{
    "SquidGame": {
        "Genre": "Thriller",
        "Rating": {
            "@Type": "Imdb",
            "#text": "8.1"
        },
        "Stars": [
            "Lee Jung-jae",
            "Park Hae-soo"
        ],
        "Budget": null
    }
}', N'<SquidGame>
      <Genre>Thriller</Genre>
      <Rating Type="Imdb">8.1</Rating>
      <Stars>Lee Jung-jae</Stars>
      <Stars>Park Hae-soo</Stars>
      <Budget />
    </SquidGame>');

SELECT * 
    , jsondata_length = DATALENGTH(jsondata), xmldata_length = DATALENGTH(xmldata)
FROM @tbl;

Output输出

+----+-----------------+----------------+
| ID | jsondata_length | xmldata_length |
+----+-----------------+----------------+
|  1 |             374 |            220 |
+----+-----------------+----------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM