首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> SQL Server 資料庫 >> 關於SqlServer

SQL Server 2005 FOR XML 進階:PATH 模式與命名空間實戰 (中)

     編輯:關於SqlServer

承接上篇,PATH 模式提供比 EXPLICIT 更簡潔的方式建構靈活 XML 結構。

PATH 模式:欄位別名即 XPath 路徑

WITH XMLNAMESPACES (DEFAULT 'https://blog.csdn.net/zhzuo')
SELECT ProductID AS 'Product/@ProductID',
       [Name] AS 'Product/Name'
FROM Production.Product
WHERE ProductSubcategoryID = 9
FOR XML PATH(''), ROOT('Products');

輸出:

<Products xmlns="https://blog.csdn.net/zhzuo">
  <Product ProductID="...">
    <Name>Rear Derailleur</Name>
  </Product>
  <Product ProductID="...">
    <Name>Front Derailleur</Name>
  </Product>
</Products>
  • PATH(''):不產生每列包裹元素,直接由別名路徑決定結構
  • @ 前綴 = 屬性,無前綴 = 子元素
  • ROOT('Products'):最外層根元素

實務整合效益

平台效益
.NET (ADO.NET)直接取得 XmlReader/XElement,省去 XSLT 轉換,直接服務上層模組
BizTalk ServerSQL Adapter 呼叫更彈性,降低 Schema 設計複雜度

注意:BizTalk 2006 R2 的 SQL Adapter Wizard 尚不支援自動從增強 FOR XML 預存程序產生 Schema,需手寫 XSD 或用 Visual Studio 設計器對應實際輸出格式。

下篇總結關鍵語法對照與最佳實務檢核表。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved