Presentation is loading. Please wait.

Presentation is loading. Please wait.

Retrieving XML Data from SQL server.  Using the FOR XML Clause to Retrieve Data Retrieving Data in XML Format How SQL Server Generates XML Using the.

Similar presentations


Presentation on theme: "Retrieving XML Data from SQL server.  Using the FOR XML Clause to Retrieve Data Retrieving Data in XML Format How SQL Server Generates XML Using the."— Presentation transcript:

1 Retrieving XML Data from SQL server

2  Using the FOR XML Clause to Retrieve Data Retrieving Data in XML Format How SQL Server Generates XML Using the FOR XML Clause Using RAW Mode to Retrieve XML Using AUTO Mode to Retrieve XML Demo: Using RAW and AUTO

3 Retrieving Data in XML Format Purchase order HTML Retailer Supplier Web site XML Customer

4 How SQL Server Generates XML ROWSET TDS/ Token XML SELECT … FROM … WHERE … FOR XML MODE SELECT … FROM … WHERE … FOR XML MODE Query processor ROWSET to XML

5 Using the FOR XML Clause The FOR XML Clause Syntax XML Document Fragments SELECT select_list FROM table_source WHERE search_condition FOR XML RAW | AUTO | EXPLICIT [, XMLDATA] [, ELEMENTS] [, BINARY BASE64] SELECT select_list FROM table_source WHERE search_condition FOR XML RAW | AUTO | EXPLICIT [, XMLDATA] [, ELEMENTS] [, BINARY BASE64]

6 Using RAW Mode to Retrieve XML Generic element produced for each row returned by the query SELECT OrderID, OrderDate FROM Orders FOR XML RAW SELECT OrderID, OrderDate FROM Orders FOR XML RAW

7 Using AUTO Mode to Retrieve XML Table name(s) are used to name elements SELECT OrderID, OrderDate FROM Orders FOR XML AUTO SELECT OrderID, OrderDate FROM Orders FOR XML AUTO

8 Demonstration: Using RAW and AUTO

9  Controlling XML Output Specifying the ELEMENTS Option Working with Multiple Tables Demo: Retrieving Data from Multiple Tables Specifying the XMLDATA Option Retrieving Binary Data

10 Specifying the ELEMENTS Option Columns are returned as child elements SELECT OrderID, OrderDate FROM Orders FOR XML AUTO, ELEMENTS SELECT OrderID, OrderDate FROM Orders FOR XML AUTO, ELEMENTS 10248 07/04/1996 10249 07/05/1996 10248 07/04/1996 10249 07/05/1996

11 Working with Multiple Tables SELECT OrderForm.OrderID, Item.ProductID Item.Quantity FROM Orders OrderForm JOIN [Order Details] Item ON OrderForm.OrderID = Item.OrderID ORDER BY OrderForm.OrderID FOR XML RAW SELECT OrderForm.OrderID, Item.ProductID Item.Quantity FROM Orders OrderForm JOIN [Order Details] Item ON OrderForm.OrderID = Item.OrderID ORDER BY OrderForm.OrderID FOR XML RAW SELECT OrderForm.OrderID, Item.ProductID Item.Quantity FROM Orders OrderForm JOIN [Order Details] Item ON OrderForm.OrderID = Item.OrderID ORDER BY OrderForm.OrderID FOR XML AUTO SELECT OrderForm.OrderID, Item.ProductID Item.Quantity FROM Orders OrderForm JOIN [Order Details] Item ON OrderForm.OrderID = Item.OrderID ORDER BY OrderForm.OrderID FOR XML AUTO

12 Demonstration: Retrieving Data from Multiple Tables

13 Specifying the XMLDATA Option XML-Data Reduced (XDR) Schema pre-pended to results SELECT OrderID, OrderDate FROM Orders FOR XML AUTO, XMLDATA SELECT OrderID, OrderDate FROM Orders FOR XML AUTO, XMLDATA <Schema name="Schema1" xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft- com:datatypes"> <ElementType name="Orders" content="empty“ model="closed"> <Schema name="Schema1" xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft- com:datatypes"> <ElementType name="Orders" content="empty“ model="closed">

14 Retrieving Binary Data SELECT EmployeeID, Photo FROM Employees WHERE EmployeeID = 10 FOR XML AUTO SELECT EmployeeID, Photo FROM Employees WHERE EmployeeID = 10 FOR XML AUTO <Employees EmployeeID="1" Photo="dbObject/Employees[@EmpID='1']/@Photo"/> <Employees EmployeeID="1" Photo="dbObject/Employees[@EmpID='1']/@Photo"/> SELECT EmployeeID, Photo FROM Employees WHERE EmployeeID = 1 FOR XML AUTO, BINARY BASE64 SELECT EmployeeID, Photo FROM Employees WHERE EmployeeID = 1 FOR XML AUTO, BINARY BASE64 <Employees EmpID="1" Photo="FRwvAAIAAAANAA4AFAAhAP////9Ca..."/> <Employees EmpID="1" Photo="FRwvAAIAAAANAA4AFAAhAP////9Ca..."/>

15  Generating Custom XML Formats Custom XML Formats Universal Tables Using EXPLICIT Mode Demonstration: Universal Tables and XML Using EXPLICIT Mode with Multiple Tables

16 Custom XML Formats Columns in a table that are mapped to an XML element can be represented as: Element values Attributes Child elements 1996-07-04T00:00:00 Queso Cabrales Singaporean Fried Mee 1996-07-04T00:00:00 Queso Cabrales Singaporean Fried Mee

17 Universal Tables Tabular representation of an XML document Tag and Parent columns determine hierarchy Column names determine element / attribute mapping TagParentInvoice!1!InvoiceNoInvoice!1!Date!ElementLineItem!2!ProductIDLineItem!2 1NULL102481996-07-04T00:00:00NULL 2110248NULL11Queso Cabrales 2110248NULL42Singaporean …

18 Using EXPLICIT Mode Construct the Transact-SQL to generate the universal table Add the FOR XML EXPLICIT clause SELECT 1 AS Tag, NULL AS Parent, OrderID AS [Invoice!1!InvoiceNo], OrderDate AS [Invoice!1!Date!Element] FROM Orders WHERE OrderID = 10248 FOR XML EXPLICIT SELECT 1 AS Tag, NULL AS Parent, OrderID AS [Invoice!1!InvoiceNo], OrderDate AS [Invoice!1!Date!Element] FROM Orders WHERE OrderID = 10248 FOR XML EXPLICIT 1996-07-04T00:00:00 1996-07-04T00:00:00

19 Using EXPLICIT Mode with Multiple Tables Use the UNION ALL operator to retrieve multiple Tag values SELECT 1 AS Tag, NULL AS Parent, OrderID AS [Invoice!1!InvoiceNo], OrderDate AS [Invoice!1!Date!Element], NULL AS [LineItem!2!ProductID], NULL AS [LineItem!2] FROM Orders WHERE OrderID=10248 UNION ALL SELECT 2 AS Tag,1 AS Parent, OD.OrderID, NULL, OD.ProductID, P.ProductName FROM [Order Details] OD JOIN Orders O ON OD.OrderID=O.OrderID JOIN Products P ON OD.ProductID = P.ProductID WHERE OD.OrderID=10248 ORDER BY [Invoice!1!InvoiceNo], [LineItem!2!ProductID] FOR XML EXPLICIT SELECT 1 AS Tag, NULL AS Parent, OrderID AS [Invoice!1!InvoiceNo], OrderDate AS [Invoice!1!Date!Element], NULL AS [LineItem!2!ProductID], NULL AS [LineItem!2] FROM Orders WHERE OrderID=10248 UNION ALL SELECT 2 AS Tag,1 AS Parent, OD.OrderID, NULL, OD.ProductID, P.ProductName FROM [Order Details] OD JOIN Orders O ON OD.OrderID=O.OrderID JOIN Products P ON OD.ProductID = P.ProductID WHERE OD.OrderID=10248 ORDER BY [Invoice!1!InvoiceNo], [LineItem!2!ProductID] FOR XML EXPLICIT

20 Demonstration: Universal Tables and XML

21 Best Practices Use RAW Mode for Aggregated Data Use AUTO Mode for Attribute-Centric or Element-Centric XML Use Aliases with AUTO and RAW to Name XML Elements and Attributes Use an ORDER BY Clause when Querying Multiple Tables Use EXPLICIT Mode for XML Documents Containing a Mix of Attribute-Centric and Element-Centric Mappings


Download ppt "Retrieving XML Data from SQL server.  Using the FOR XML Clause to Retrieve Data Retrieving Data in XML Format How SQL Server Generates XML Using the."

Similar presentations


Ads by Google