XQuery 擴充函式 - sql:column()

適用於:SQL Server

如系結關聯式資料內部 XML 主題 所述,當您使用 XML 資料類型方法 在 XQuery 內公開關系型值時,可以使用 sql:column() 函式。

例如, query() 方法 (XML 資料類型) 是用來針對儲存在 xml 類型的變數或資料行中的 XML 實例指定查詢。 有時候,您可能也會希望查詢使用來自另一個非 XML 資料行的值,將關聯式和 XML 資料結合在一起。 若要這樣做,請使用 sql:column() 函式。

SQL 值會對應至對應的 XQuery 值,而且其類型會是相當於對應 SQL 類型的 XQuery 基底類型。

語法

  
sql:column("columnName")  

備註

請注意,XQuery 內 sql:column() 函式中所 指定資料行的參考是指正在處理的資料列中的資料行。

在 SQL Server 中,您只能參考 XML-DML insert 語句之來源運算式內容中的 xml 實例;否則,您無法參考 xml 或 CLR 使用者定義型別的資料 行。

JOIN 作業不支援 sql:column() 函式。 您可以改用 APPLY 作業。

範例

A. 使用 sql:column() 在 XML 內擷取關聯式值

在建構 XML 時,下列範例說明如何從非 XML 關聯式資料行擷取值,以系結 XML 和關聯式資料。

此查詢會建構具有下列格式的 XML:

<Product ProductID="771" ProductName="Mountain-100 Silver, 38" ProductPrice="3399.99" ProductModelID="19"   
  ProductModelName="Mountain 100" />  

請注意下列建構的 XML:

  • ProductID、ProductName ProductPrice 屬性值是從 Product 資料表取得

  • ProductModelID 屬性值是從 ProductModel 資料表擷 取。

  • 為了讓查詢更有趣, ProductModelName 屬性值是從 xml 類型的 CatalogDescription 資料行 取得 。 因為 XML 產品模型目錄資訊並未儲存給所有產品模型, if 所以語句只會在存在時擷取值。

    SELECT P.ProductID, CatalogDescription.query('  
    declare namespace pd="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
           <Product   
               ProductID=       "{ sql:column("P.ProductID") }"  
               ProductName=     "{ sql:column("P.Name") }"  
               ProductPrice=    "{ sql:column("P.ListPrice") }"  
               ProductModelID= "{ sql:column("PM.ProductModelID") }" >  
               { if (not(empty(/pd:ProductDescription))) then  
                 attribute ProductModelName { /pd:ProductDescription[1]/@ProductModelName }  
                else   
                   ()  
    }  
            </Product>  
    ') as Result  
    FROM Production.ProductModel PM, Production.Product P  
    WHERE PM.ProductModelID = P.ProductModelID  
    AND   CatalogDescription is not NULL  
    ORDER By PM.ProductModelID  
    

請注意下列項目是從上一個查詢而來:

  • 因為值是從兩個不同的資料表擷取,FROM 子句會指定兩個數據表。 WHERE 子句中的條件會篩選結果,並只擷取產品模型具有目錄描述的產品。

  • XQuery Prolog 中的 namespace 關鍵字會定義用於查詢主體的 XML 命名空間前置詞 「pd」。 請注意,資料表別名 「P」 和 「PM」 定義于查詢本身的 FROM 子句中。

  • sql:column() 函式可用來將非 XML 值帶入 XML 內。

以下是部份結果:

ProductID               Result  
-----------------------------------------------------------------  
771         <Product ProductID="771"                   ProductName="Mountain-100 Silver, 38"   
                  ProductPrice="3399.99" ProductModelID="19"   
                  ProductModelName="Mountain 100" />  
...  

下列查詢會建構包含產品特定資訊的 XML。 此資訊包括 ProductID、ProductName、ProductPrice,如果可用,則屬於特定產品型號 ProductModelID=19 的所有產品的 ProductModelName。 然後,XML 會指派給 @x xml 類型的變數

declare @x xml  
SELECT @x = CatalogDescription.query('  
declare namespace pd="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
       <Product   
           ProductID=       "{ sql:column("P.ProductID") }"  
           ProductName=     "{ sql:column("P.Name") }"  
           ProductPrice=    "{ sql:column("P.ListPrice") }"  
           ProductModelID= "{ sql:column("PM.ProductModelID") }" >  
           { if (not(empty(/pd:ProductDescription))) then  
             attribute ProductModelName { /pd:ProductDescription[1]/@ProductModelName }  
            else   
               ()  
}  
        </Product>  
')   
FROM Production.ProductModel PM, Production.Product P  
WHERE PM.ProductModelID = P.ProductModelID  
And P.ProductModelID = 19  
select @x  

另請參閱

SQL Server XQuery 延伸模組函式
比較具類型的 XML 與不具類型的 XML
XML 資料 (SQL Server)
建立 XML 資料的執行個體
xml 資料類型方法
XML 資料修改語言 (XML DML)