共用方式為


建立 XML 結構描述集合 (Transact-SQL)

適用於:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceMicrosoft Fabric 中的 SQL 資料庫

將結構描述元件匯入資料庫中。

Transact-SQL 語法慣例

語法

CREATE XML SCHEMA COLLECTION [ <relational_schema>. ] sql_identifier AS Expression

引數

relational_schema

識別關聯式結構描述名稱。 若未指定,則會假設使用預設的關聯式結構描述。

sql_identifier

XML 綱目集合的 SQL 識別碼。

運算式

字串常數或純量變數。 是 varcharvarbinarynvarcharxml 類型。

備註

您也可以利用 ALTER XML SCHEMA COLLECTION 將新命名空間加入集合中,或將新元件加入集合的現有命名空間中。

若要移除集合,請使用 DROP XML SCHEMA COLLECTION。

權限

若要建立 XML SCHEMA COLLECTION,至少需要下列權限集合之一:

  • CONTROL 伺服器上的權限
  • ALTER ANY DATABASE 伺服器上的權限
  • ALTER 資料庫的權限
  • CONTROL 資料庫中的權限
  • ALTER ANY SCHEMA 資料庫中的權限和 CREATE XML SCHEMA COLLECTION 權限
  • ALTERCONTROL 關聯式結構描述的權限和 CREATE XML SCHEMA COLLECTION 資料庫中的權限

範例

A. 在資料庫中建立 XML 結構描述集合

下列範例會建立 XML 結構描述集合 ManuInstructionsSchemaCollection。 這個集合只有一個結構描述命名空間。

-- Create a sample database in which to load the XML schema collection.
CREATE DATABASE SampleDB;
GO

USE SampleDB;
GO

CREATE XML SCHEMA COLLECTION ManuInstructionsSchemaCollection
    AS N'<?xml version="1.0" encoding="UTF-16"?>
<xsd:schema targetNamespace="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
   xmlns          ="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
   elementFormDefault="qualified"
   attributeFormDefault="unqualified"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

    <xsd:complexType name="StepType" mixed="true" >
        <xsd:choice  minOccurs="0" maxOccurs="unbounded" >
            <xsd:element name="tool" type="xsd:string" />
            <xsd:element name="material" type="xsd:string" />
            <xsd:element name="blueprint" type="xsd:string" />
            <xsd:element name="specs" type="xsd:string" />
            <xsd:element name="diag" type="xsd:string" />
        </xsd:choice>
    </xsd:complexType>

    <xsd:element  name="root">
        <xsd:complexType mixed="true">
            <xsd:sequence>
                <xsd:element name="Location" minOccurs="1" maxOccurs="unbounded">
                    <xsd:complexType mixed="true">
                        <xsd:sequence>
                            <xsd:element name="step" type="StepType" minOccurs="1" maxOccurs="unbounded" />
                        </xsd:sequence>
                        <xsd:attribute name="LocationID" type="xsd:integer" use="required"/>
                        <xsd:attribute name="SetupHours" type="xsd:decimal" use="optional"/>
                        <xsd:attribute name="MachineHours" type="xsd:decimal" use="optional"/>
                        <xsd:attribute name="LaborHours" type="xsd:decimal" use="optional"/>
                        <xsd:attribute name="LotSize" type="xsd:decimal" use="optional"/>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>';
GO

-- Verify - list of collections in the database.
SELECT *
FROM sys.xml_schema_collections;

-- Verify - list of namespaces in the database.
SELECT name
FROM sys.xml_schema_namespaces;

-- Use it. Create a typed xml variable. Note collection name specified.
DECLARE @x AS XML(ManuInstructionsSchemaCollection);
GO

--Or create a typed xml column.
CREATE TABLE T
(
    i INT PRIMARY KEY,
    x XML(ManuInstructionsSchemaCollection)
);
GO

-- Clean up
DROP TABLE T;
GO

DROP XML SCHEMA COLLECTION ManuInstructionsSchemaCollection;
GO

USE master;
GO

DROP DATABASE SampleDB;

或者,您可以指派結構描述集合到變數中,並在 CREATE XML SCHEMA COLLECTION 陳述式中指定變數,如下所示:

DECLARE @MySchemaCollection AS NVARCHAR (MAX);

SET @MySchemaCollection = N' copy the schema collection here';

CREATE XML SCHEMA COLLECTION MyCollection
    AS @MySchemaCollection;

範例中的變數屬於 nvarchar(max) 類型。 變數也可以是 xml 資料類型,在這種情況下,它會隱含地轉換為字串。

如需詳細資訊,請參閱 檢視預存的 XML 結構描述集合

您可以將綱目集合儲存在 xml 類型直欄中。 在此情況下,若要建立 XML 綱目集合,請執行下列步驟:

  1. 使用陳述式從 SELECT 資料行擷取綱目集合,並將它指派給 xml 類型的變數或 varchar 類型。

  2. 在陳述式中 CREATE XML SCHEMA COLLECTION 指定變數名稱。

只會 CREATE XML SCHEMA COLLECTION 儲存 SQL Server 瞭解的結構描述元件;XML 結構描述中的所有內容都不會儲存在資料庫中。 因此,如果您想要將 XML 結構描述集合完全恢復為提供方式,您應該將 XML 結構描述儲存在資料庫資料行或電腦上的其他資料夾中。

B. 在結構描述集合中指定多個結構描述命名空間

建立 XML 結構描述集合時,您也可以指定多個 XML 結構描述。 例如:

CREATE XML SCHEMA COLLECTION MyCollection
    AS N'
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- Contents of schema here -->
</xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- Contents of schema here -->
</xsd:schema>';

下列範例會建立包括兩個 XML 結構描述命名空間的 XML 結構描述集合 ProductDescriptionSchemaCollection

CREATE XML SCHEMA COLLECTION ProductDescriptionSchemaCollection
    AS '<xsd:schema targetNamespace="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain"
    xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain"
    elementFormDefault="qualified"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
    <xsd:element name="Warranty"  >
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="WarrantyPeriod" type="xsd:string"  />
                <xsd:element name="Description" type="xsd:string"  />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>
 <xs:schema targetNamespace="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription"
    xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription"
    elementFormDefault="qualified"
    xmlns:mstns="https://tempuri.org/XMLSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:wm="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain" >
    <xs:import
namespace="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain" />
    <xs:element name="ProductDescription" type="ProductDescription" />
        <xs:complexType name="ProductDescription">
            <xs:sequence>
                <xs:element name="Summary" type="Summary" minOccurs="0" />
            </xs:sequence>
            <xs:attribute name="ProductModelID" type="xs:string" />
            <xs:attribute name="ProductModelName" type="xs:string" />
        </xs:complexType>
        <xs:complexType name="Summary" mixed="true" >
            <xs:sequence>
                <xs:any processContents="skip" namespace="http://www.w3.org/1999/xhtml" minOccurs="0" maxOccurs="unbounded" />
            </xs:sequence>
        </xs:complexType>
</xs:schema>';
GO

-- Clean up
DROP XML SCHEMA COLLECTION ProductDescriptionSchemaCollection;
GO

C. 匯入未指定目標命名空間的結構描述

如果在集合中匯入不包含 targetNamespace 屬性的結構描述,則其元件會與空白字串目標命名空間相關聯,如下列範例所示。 未關聯集合中匯入的一或多個結構描述會導致多個結構描述元件 (可能不相關) 與預設空字串命名空間相關聯。

-- Create a collection that contains a schema with no target namespace.
CREATE XML SCHEMA COLLECTION MySampleCollection
    AS '
<schema xmlns="http://www.w3.org/2001/XMLSchema"  xmlns:ns="http://ns">
<element name="e" type="dateTime"/>
</schema>';
GO

-- Query will return the names of all the collections that
--contain a schema with no target namespace.
SELECT sys.xml_schema_collections.name
FROM sys.xml_schema_collections
     INNER JOIN sys.xml_schema_namespaces
         ON sys.xml_schema_collections.xml_collection_id = sys.xml_schema_namespaces.xml_collection_id
WHERE sys.xml_schema_namespaces.name = '';

D. 使用 XML 結構描述集合和批次

架構集合無法在建立結構描述集合的相同批次中參考。 如果您嘗試在建立集合的相同批次中參考集合,您會收到錯誤,指出集合不存在。 以下示例有效;不過,如果您移除 GO 並因此嘗試參照 XML 綱目集合,以在同一批次中鍵入 XML 變數,則會傳回錯誤。

CREATE XML SCHEMA COLLECTION mySC
    AS '
<schema xmlns="http://www.w3.org/2001/XMLSchema">
      <element name="root" type="string"/>
</schema>
';
GO

CREATE TABLE T
(
    Col1 XML(mySC)
);
GO