Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisenriquecorona/cff5772220ea3b34285b9eea9dfefe62 to your computer and use it in GitHub Desktop.
Save luisenriquecorona/cff5772220ea3b34285b9eea9dfefe62 to your computer and use it in GitHub Desktop.
To demonstrate the use of the XQuery methods in this chapter, we will create a table in the WideWorldImporters database, called Sales. CustomerOrderSummary. This table can be created using the script
USE WideWorldImporters
GO
CREATE TABLE Sales.CustomerOrderSummary
(
ID INT NOT NULL IDENTITY,
CustomerID INT NOT NULL,
OrderSummary XML
);
INSERT INTO Sales.CustomerOrderSummary (CustomerID,
OrderSummary)
SELECT
CustomerID,
(
SELECT
CustomerName 'OrderHeader/CustomerName'
, OrderDate 'OrderHeader/OrderDate' , OrderID 'OrderHeader/OrderID'
,(
SELECT
LineItems2.StockItemID
'@ProductID'
, StockItems.StockItem
Name '@ProductName'
, LineItems2.UnitPrice
'@Price'
, Quantity '@Qty'
FROM Sales.OrderLines LineItems2 INNER JOIN Warehouse.StockItems StockItems
ON LineItems2.StockItemID
= StockItems.StockItemID WHERE LineItems2.OrderID =
Base.OrderID
FOR XML PATH('Product'), TYPE
) 'OrderDetails'
FROM
(
SELECT DISTINCT
Customers.CustomerName
, SalesOrder.OrderDate
, SalesOrder.OrderID
FROM Sales.Orders SalesOrder
INNER JOIN Sales.OrderLines LineItem
ON SalesOrder.OrderID =
LineItem.OrderID
INNER JOIN Sales.Customers Customers
ON Customers.CustomerID =
SalesOrder.CustomerID
WHERE customers.CustomerID = OuterCust.
CustomerID
) Base
FOR XML PATH('Order'), ROOT ('SalesOrders'), TYPE
) AS OrderSummary
FROM Sales.Customers OuterCust ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment