SQL Server XML output

I. Overview

SELECT query results as a set of rows returned. Specify the FOR XML clause in SQL query to the official results as an XML query to retrieve. FOR XML clause can be used in the top-level query and sub-queries. Top FOR XML clause can only be used in the SELECT statement. In the subquery, FOR XML can be used INSERT, UPDATE, and DELETE statements. It can also be used in the assignment statement.
  In the FOR XML clause, specify one of the following modes:

  • RAW
  • AUTO
  • EXPLICIT
  • PATH

Second, the test data

to build a table: the TestTable, data in the table below:

 

 

Third, examples

. 1, the FOR the XML the RAW
  the RAW mode will be returned rows SELECT statement to generate a set per line <row> element. You can generate XML hierarchies of nested FOR XML queries by writing.

SQL语句:
SELECT ID, Name, Age, Sex FROM TestTable FOR XML RAW

执行结果:
<row ID="1" Name="Jim" Age="20" Sex="男" />
<row ID="2" Name="Tina" Age="23" Sex="女" />

2, the FOR XML the AUTO
  the AUTO mode will be used based on the specified heuristic generated in a manner nested SELECT statements in the XML results. That you have minimal control over the shape of the XML generated. In addition to the shape of XML AUTO mode tentative generation method, you can also write FOR XML queries to generate XML hierarchy.

SQL statements:
the SELECT ID, the Name, Age, Sex the FROM the TestTable the FOR XML AUTO

the results:
<the TestTable ID = "1" the Name = "Jim" Age = "20" Sex = "M" />
<the TestTable ID = "2" Name = "Tina" Age = " 23" Sex = " female" />

NOTE: XML structure of this mode of return is not controllable, with the SQL database model sample as follows:

SQL语句:
USE AdventureWorks2012
GO
SELECT Cust.CustomerID,
OrderHeader.CustomerID,
OrderHeader.SalesOrderID,
OrderHeader.Status
FROM Sales.Customer Cust
INNER JOIN Sales.SalesOrderHeader OrderHeader
ON Cust.CustomerID = OrderHeader.CustomerID
FOR XML AUTO;

执行结果:
<Cust CustomerID="29825">
<OrderHeader CustomerID="29825" SalesOrderID="43659" Status="5" />
</Cust>
<Cust CustomerID="29672">
<OrderHeader CustomerID="29672" SalesOrderID="43660" Status="5" />
</Cust>

Field slightly changing the order of the SELECT, the result will change;
the SQL statement:
the USE the AdventureWorks2012
the GO
SELECT OrderHeader.CustomerID,
OrderHeader.SalesOrderID,
OrderHeader.Status,
Cust.CustomerID
the FROM the Sales.Customer the Cust
the INNER the JOIN the Sales.SalesOrderHeader the OrderHeader
the ON the Cust. = OrderHeader.CustomerID CustomerID
the FOR XML AUTO;

执行结果:
<OrderHeader CustomerID="29825" SalesOrderID="43659" Status="5">
<Cust CustomerID="29825" />
</OrderHeader>
<OrderHeader CustomerID="29672" SalesOrderID="43660" Status="5">
<Cust CustomerID="29672" />
</OrderHeader>

. 3, the FOR XML EXPLICIT
  EXPLICIT mode allows more control over the shape of XML. You can freely mix attributes and elements to determine the shape of XML. Since the implementation of the query results generated set of rows need to have a specific format. This rowset XML format is then mapped to the shape. EXPLICIT mode can freely use the mixing elements and attributes, create packaging and nested complex property, create a value separated by spaces (e.g. OrderID attribute may have a value sort order ID), and mixed content.
  However, writing EXPLICIT mode queries would be more trouble. May use certain features new FOR XML (e.g. write nested FOR XML RAW / AUTO / PATH TYPE command and query mode), without using EXPLICIT mode to generate a hierarchy. Nested FOR XML queries can be generated using any XML EXPLICIT mode can generate. For more information, see Using FOR XML queries and nested FOR XML queries TYPE command.

4, the PATH FOR XML
  the PATH mode with nested FOR XML query capabilities together provide the flexibility EXPLICIT mode in a relatively simple manner.

SQL statements:
the SELECT ID, the Name, Age, Sex the FROM the TestTable the FOR XML the PATH ( 'Info')

执行结果:
<Info>
<ID>1</ID>
<Name>Jim</Name>
<Age>20</Age>
<Sex>男</Sex>
</Info>
<Info>
<ID>2</ID>
<Name>Tina</Name>
<Age>23</Age>
<Sex>女</Sex>
</Info>

ID as the attribute;
the SQL statement:
the SELECT ID AS '@ID', the Name, Age, Sex the FOR the XML the PATH the TestTable the FROM ( 'Info')

执行结果:
<Info ID="1">
<Name>Jim</Name>
<Age>20</Age>
<Sex>男</Sex>
</Info>
<Info ID="2">
<Name>Tina</Name>
<Age>23</Age>
<Sex>女</Sex>
</Info>

Four, ELEMENTS
  ELEMENTS specified column returned as child elements, namely ELEMENTS option is specified XML result is element-centric. Otherwise, they are mapped to XML attributes. This option is only supported in RAW, AUTO, and PATH modes.

SQL语句:
USE AdventureWorks2012
GO
SELECT Cust.CustomerID,
OrderHeader.CustomerID,
OrderHeader.SalesOrderID,
OrderHeader.Status
FROM Sales.Customer Cust
INNER JOIN Sales.SalesOrderHeader OrderHeader
ON Cust.CustomerID = OrderHeader.CustomerID
FOR XML AUTO,ELEMENTS

执行结果:
<Cust>
<CustomerID>29825</CustomerID>
<OrderHeader>
<CustomerID>29825</CustomerID>
<SalesOrderID>43659</SalesOrderID>
<Status>5</Status>
</OrderHeader>
</Cust>
<Cust>
<CustomerID>29672</CustomerID>
<OrderHeader>
<CustomerID>29672</CustomerID>
<SalesOrderID>43660</SalesOrderID>
<Status>5</Status>
</OrderHeader>
</Cust>

Five, FOR XML query TYPE command

SQL Server XML data type instance data as a different server configurations (e.g., using TYPE directive FOR XML query, or wherein the xml data type returned FOR XML query instance data values SQL table columns and output parameters in XML) returns the result to the client. In the client application code, ADO.NET provider requests this type of XML data in binary coded information from the server. However, if the FOR XML TYPE directive without use, the XML data will return the string type. In any case, the client access interfaces have always been able to handle either form of XML contents. Please note that the top-level FOR XML without the TYPE directive can not be used in conjunction with a cursor.
If TYPE directive is not specified, the type FOR XML query results returned is nvarchar (max).

SELECT ID, Name, Age, Sex FROM TestTable FOR XML AUTO, TYPE

Guess you like

Origin www.cnblogs.com/Juning/p/11502234.html