SQL Server與XML 數據

  Server中的FOR XML語句是擴展SELECT的語法,它主要返回XML代替了行和列,可配置用於返回屬性,元素和架構其常用RAW 、AUTO、PATH模式查詢.
USE [Northwind]
GO
CREATE PROCEDURE [dbo].[usp_GetProductsXmlAuto]
AS
--demo1
SELECT [ProductID]
      ,[ProductName]
      ,[SupplierID]
      ,[CategoryID]
      ,[QuantityPerUnit]
      ,[UnitPrice]
      ,[UnitsInStock]
      ,[UnitsOnOrder]
      ,[ReorderLevel]
      ,[Discontinued]
  FROM [Products] 
  FOR XML AUTO,ROOT('Products')
--demo2 
--SELECT [ProductID]
--    ,[ProductName]
--    ,[SupplierID]
--    ,[CategoryID]
--    ,[QuantityPerUnit]
--    ,[UnitPrice]
--    ,[UnitsInStock]
--    ,[UnitsOnOrder]
--    ,[ReorderLevel]
--    ,[Discontinued]
--FROM [Products] 
--FOR XML RAW('Product'),ROOT('Products')
--demo3 
--SELECT     Products.ProductName,
--		   Products.ProductID, 
--		   Products.SupplierID, 
--		   OrderDetails.UnitPrice, 
--		   OrderDetails.Quantity, 
--           OrderDetails.Discount
--FROM       OrderDetails INNER JOIN
--           Products ON OrderDetails.ProductID = Products.ProductID
-- ORDER BY Products.ProductID
--  FOR XML AUTO,ROOT('Orders')




USE [Northwind]
GO
CREATE PROCEDURE [dbo].[usp_GetProductsXmlXPath]
AS
--demo1
--SELECT [ProductID]
--      ,[ProductName]
--      ,[SupplierID]
--      ,[CategoryID]
--      ,[QuantityPerUnit]
--      ,[UnitPrice]
--      ,[UnitsInStock]
--      ,[UnitsOnOrder]
--      ,[ReorderLevel]
--      ,[Discontinued]
--  FROM [Products] 
--  FOR XML Path,ROOT('Products')
 --demo2
  SELECT [ProductID]
      ,[ProductName]
      ,[SupplierID]
      ,[CategoryID]
      ,[QuantityPerUnit]
      ,[UnitPrice]
      --,[UnitsInStock]
      --,[UnitsOnOrder]
      --,[ReorderLevel]
      --,[Discontinued]
  FROM [Products] 
  FOR XML Path('Category'),ROOT('Products')       
 

發佈了81 篇原創文章 · 獲贊 0 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章