Loading the XML Datatype
How do you load XML data into the new XML datatype? With Yukon, you can convert string data to XML data implicitly or explicitly by using the T-SQL functions CAST and CONVERT. Take a look:
INSERT INTO Customers (ID, CustomerDocument)
VALUES
(
1,
'<Customer
xmlns="http://www.csharp.at/Conferences/
2004/02/ASPKonferenz"
CustomerID="ALFKI" State="DE" Type="ISV">
Alfreds Futterkiste
</Customer>'
)
INSERT INTO Customers(ID, CustomerDocument)
VALUES
(
2,
CAST('<Customer xmlns="http://www.csharp.at/
Conferences/2004/02/ASPKonferenz"
CustomerID="MSFT" State="WA" Type="ISV">
Microsoft Corporation
</Customer>' AS xml)
)
INSERT INTO Customers(ID, CustomerDocument)
VALUES
(
3,
CONVERT(xml, '<Customer xmlns="http://www.csharp.at/
Conferences/2004/02/ASPKonferenz"
CustomerID="TT" State="AT" Type="ISV">
TechTalk
</Customer>')
)
You need to provide the target namespace in order to insert XML data to a typed XML datatype. Without the the target namespace, Yukon raises an exception and the data won't be inserted. This guarantees that all inserted XML data is validated and matches the provided XML schema.