IT tutorials
 
Database
 

SQL Server 2012 : Using XML Data - FOR XML (part 2) - Raw

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

Raw

FOR XML RAW is similar to FOR XML AUTO but with several differences. First, FOR XML AUTO does not enable you to alter the name of the elements in the resulting XML. Also, as you saw in the previous section, FOR XML AUTO names the elements after the name of the table or alias.

For example, the following code illustrates a simple FOR XML RAW clause:

SELECT CustomerID, OrderNumber, OrderDate
FROM Orders
FOR XML RAW

/*
<row CustomerID="1" OrderNumber="10001" OrderDate="2011-06-15T00:00:00" />
<row CustomerID="2" OrderNumber="10002" OrderDate="2011-06-16T00:00:00" />
<row CustomerID="1" OrderNumber="10003" OrderDate="2011-06-17T00:00:00" />
<row CustomerID="2" OrderNumber="10004" OrderDate="2011-06-18T00:00:00" />
*/

By default, FOR XML RAW generates elements named <row> but enables you to rename it if you want via an optional element name of the RAW directive:

SELECT CustomerID, OrderNumber, OrderDate
FROM Orders
FOR XML RAW('Order')

/*
<Order CustomerID="1" OrderNumber="10001" OrderDate="2011-06-15T00:00:00" />
<Order CustomerID="2" OrderNumber="10002" OrderDate="2011-06-16T00:00:00" />
<Order CustomerID="1" OrderNumber="10003" OrderDate="2011-06-17T00:00:00" />
<Order CustomerID="2" OrderNumber="10004" OrderDate="2011-06-18T00:00:00" />
*/

In the following example, notice that table names in the query are both aliased, but because no name is specified in the RAW directive, each row is still named <row>.

SELECT o.OrderNumber, o.OrderDate, c.Name
FROM Orders o
INNER JOIN Customer c ON o.CustomerID = c.CustomerID
FOR XML RAW

/*
<row OrderNumber="10001" OrderDate="2011-06-15T00:00:00" Name="Scott" />
<row OrderNumber="10002" OrderDate="2011-06-16T00:00:00" Name="Adam" />
<row OrderNumber="10003" OrderDate="2011-06-17T00:00:00" Name="Scott" />
<row OrderNumber="10004" OrderDate="2011-06-18T00:00:00" Name="Adam" />
*/

The RAW directive also enables you to specify an optional ROOT directive (similar to the AUTO directive), which generates a root element with the specified name.

SELECT o.OrderNumber, o.OrderDate, c.Name
FROM Orders o
INNER JOIN Customer c ON o.CustomerID = c.CustomerID
FOR XML RAW('Order'), ROOT('Orders')

/*
<Orders>
<Order OrderNumber="10001" OrderDate="2011-06-15T00:00:00" Name="Scott" />
<Order OrderNumber="10002" OrderDate="2011-06-16T00:00:00" Name="Adam" />
<Order OrderNumber="10003" OrderDate="2011-06-17T00:00:00" Name="Scott" />
<Order OrderNumber="10004" OrderDate="2011-06-18T00:00:00" Name="Adam" />
</Orders>
/*

If you specify the ROOT directive without including a name for the ROOT element, a top-level root element named <root> generates.

The RAW directive also accepts the ELEMENTS directive:

SELECT o.OrderNumber, o.OrderDate, c.Name
FROM Orders o
INNER JOIN Customer c ON o.CustomerID = c.CustomerID
FOR XML RAW('Order'), ROOT('Orders'), ELEMENTS

/*
<Orders>
<Order>
<OrderNumber>10001</OrderNumber>
<OrderDate>2011-06-15T00:00:00</OrderDate>
<Name>Scott</Name>
</Order>
<Order>
<OrderNumber>10002</OrderNumber>
<OrderDate>2011-06-16T00:00:00</OrderDate>
<Name>Adam</Name>
</Order>
<Order>
<OrderNumber>10003</OrderNumber>
<OrderDate>2011-06-17T00:00:00</OrderDate>
<Name>Scott</Name>
</Order>
<Order>
<OrderNumber>10004</OrderNumber>
<OrderDate>2011-06-18T00:00:00</OrderDate>
<Name>Adam</Name>
</Order>
</Orders>
/*
 
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