laika222
11/5/2017 - 3:20 PM

XML Datatype and Storing XML in SQL Server

-- XML is a native data type where you can place well-formed XML documents or XML fragments. The XML isn't stored as a literal string of XML, but is stored as a tree. When you query the XML back out, it rebuilds the XML into code that is semantically the same as what you put in, though there might be some differences (attributes in different orders, comments being lost, etc.).

-- example of how to create table that has an OrderItems column which will store XML, alongside the other columns which are normal relational data
CREATE TABLE #SalesOrder
(OrderID INT IDENTITY,
OrderDate DATETIME DEFAULT GETDATE(),
OrderItems XML);

-- example of how to insert XML into the OrderItems column. Note that this is XML fragment data, not an entire well-formed XML document
INSERT INTO SalesOrderTest (OrderItems)
VALUES
('<item id="561" quantity="1"> </item>
<item id="127" quantity="2"> </item>' );