共用方式為


concat 函數 (XQuery)

接受零個或多個字串做為引數,並傳回串連每個引數的值所建立的字串。

語法

fn:concat ($string as xs:string?
           ,$string as xs:string?
           [, ...]) as xs:string

引數

  • $string
    要串連的選擇性字串。

備註

函式至少需要兩個引數。 如果引數是空白時序,將以零長度的字串處理。

補充字元 (Surrogate 字組)

XQuery 函式中 Surrogate 字組的行為相依於資料庫相容性層級,而且在某些情況下,還相依於函式的預設命名空間 URI。 如需詳細資訊,請參閱<SQL Server 2012 中對於 Database Engine 的重大變更>主題中的「XQuery 函式是 Surrogate 感知的」一節。 另請參閱<ALTER DATABASE 相容性層級 (Transact-SQL)>和<定序與 Unicode 支援>。

範例

本主題是針對 XML 執行個體提供 XQuery 範例,這些執行個體是儲存在 AdventureWorks 範例資料庫的各個 xml 類型的資料行中。

A.使用 concat() XQuery 函式串連字串

對於特定產品型號,此查詢將傳回透過串連警告期限與警告描述所建立的字串。 在目錄描述文件中,<Warranty> 元素是由 <WarrantyPeriod> 與 <Description> 子元素所構成。

WITH XMLNAMESPACES (
'https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription' AS pd,
'https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain' AS wm)
SELECT CatalogDescription.query('
    <Product 
        ProductModelID= "{ (/pd:ProductDescription/@ProductModelID)[1] }"
        ProductModelName = "{ sql:column("PD.Name") }" >
        { 
          concat( string((/pd:ProductDescription/pd:Features/wm:Warranty/wm:WarrantyPeriod)[1]), "-",
                  string((/pd:ProductDescription/pd:Features/wm:Warranty/wm:Description)[1])) 
         } 
     </Product>
 ') as Result
FROM Production.ProductModel PD
WHERE  PD.ProductModelID=28

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

  • 在 SELECT 子句中,CatalogDescription 是 xml 類型的資料行。 因此,將會使用 query() 方法 (XML 資料類型)、Instructions.query()。 XQuery 陳述式是指定成查詢方法的引數。

  • 查詢所執行的文件將會使用命名空間。 因此,namespace 關鍵字是用以定義命名空間的前置詞。 如需詳細資訊,請參閱<XQuery 初構>。

以下是結果:

<Product ProductModelID="28" ProductModelName="Road-450">1 year-parts and labor</Product>

下列查詢會擷取特定產品的資訊。 下列查詢會擷取儲存 XML 目錄描述的所有產品之相同資訊。 如果資料列中的 XML 文件有 <ProductDescription> 元素,WHERE 子句中 xml 資料類型的 exist() 方法就會傳回 True。

WITH XMLNAMESPACES (
'https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription' AS pd,
'https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain' AS wm)

SELECT CatalogDescription.query('
    <Product 
        ProductModelID= "{ (/pd:ProductDescription/@ProductModelID)[1] }" 
        ProductName = "{ sql:column("PD.Name") }" >
        { 
          concat( string((/pd:ProductDescription/pd:Features/wm:Warranty/wm:WarrantyPeriod)[1]), "-",
                  string((/pd:ProductDescription/pd:Features/wm:Warranty/wm:Description)[1])) 
         } 
     </Product>
 ') as Result
FROM Production.ProductModel PD
WHERE CatalogDescription.exist('//pd:ProductDescription ') = 1

請注意 xml 類型的 exist() 方法所傳回的布林值是與 1 比較。

實作限制

以下為其限制:

  • 在 SQL Server 中的 concat() 函數只接受 xs:string 類型的值。 其他的值必須明確地轉換成 xs:string 或 xdt:untypedAtomic。

請參閱

參考

針對 xml 資料類型的 XQuery 函數