XML Columns and Variables
We have seen through the previous
examples how to use the XML data type in both columns and variables.
You created a table with an XML data type column, created an XSD
schema, and from that created an XML Schema Collection and applied the
schema collection to the table. If you have everything created
beforehand, you can apply the schema collection during the creation of
the table:
DROP TABLE ItemInfo
GO
CREATE TABLE [dbo].[ItemInfo](
[OrderID] [int] NOT NULL,
[ItemData] [xml](CONTENT [dbo].[OrderInfoSchemaCollection]) NULL
) ON [PRIMARY]
GO
You can also apply the schema collection with an XML variable:
DECLARE @var xml (OrderInfoSchemaCollection)
SET @var =
'<Root>
<Junk1>Some Junk</Junk1>
<Junk2>Some More Junk</Junk2>
<Junk3>Even More Junk</Junk3>
<Junk4>Too Much Junk</Junk4>
</Root>'
SELECT @var
Now obviously the preceding statement fails because the XML is invalid, but the next statement succeeds:
DECLARE @var xml (OrderInfoSchemaCollection)
SET @var =
'<Order OrderID="1">
<Item>
<ItemNumber>V001</ItemNumber>
<Quantity>1</Quantity>
<Price>299.99</Price>
</Item>
</Order>'
SELECT @var
It is also possible to initialize an XML variable from the results of a FOR XML query:
DECLARE @var xml
SET @var = (SELECT OrderID FROM Orders FOR XML AUTO)
SELECT @var
You can also initialize XML variables a number of
ways, such as by static XML strings, from another XML variable, or from
the return value of a function.
XML Parameters and Return Values
Both typed and untyped XML parameters can be passed to a stored procedure as INPUT as well as OUTPUT
parameters. XML parameters can even be used as arguments, including as
return values of scalar functions or the results of a table-valued
function.
The following example illustrates how to use the XML data type as an input parameter to a stored procedure:
CREATE PROCEDURE SampleProc
(
@var xml
)
AS
SELECT @var
EXEC SampleProc ‘<Root>
<Junk1>Some Junk</Junk1>
<Junk2>Some More Junk</Junk2>
<Junk3>Even More Junk</Junk3>
<Junk4>Too Much Junk</Junk4>
</Root>'
The following example illustrates how to apply an
XML schema to an input parameter. The first EXEC statement fails while
the second succeeds because the second XML fragment passes validation.
CREATE PROCEDURE SampleProc2
(
@var xml (OrderInfoSchemaCollection)
)
AS
SELECT @var
EXEC SampleProc2 ‘<Root>
<Junk1>Some Junk</Junk1>
<Junk2>Some More Junk</Junk2>
<Junk3>Even More Junk</Junk3>
<Junk4>Too Much Junk</Junk4>
</Root>'
EXEC SampleProc2 ‘
<Order OrderID="1">
<Item>
<ItemNumber>V001</ItemNumber>
<Quantity>1</Quantity>
<Price>299.99</Price>
</Item>
</Order>'
When a function returns an XML data type value, XML data type methods can be directly called on the return value:
CREATE FUNCTION XMLFunc
(
@var int
) RETURNS xml
AS
BEGIN
DECLARE @val xml
SET @val = (SELECT OrderID, CustomerID
FROM Orders
WHERE OrderID = @var
FOR XML PATH(''), ROOT('OrderInfo'))
RETURN @val
END
GO
SELECT dbo.XMLFunc(1).value('(OrderInfo/CustomerID)[1]', ‘INT')
as customer
The previous example is a nice segue into the next session discussing the XML data type methods.