IT tutorials
 
Database
 

SQL Server 2012 : The XML Data Type (part 3) - XML Columns and Variables, XML Parameters and Return Values

2/10/2014 12:37:50 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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.

 
Others
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us