다음을 통해 공유


XMLDATA 및 XMLSCHEMA를 사용하여 결과로 스키마 요청

적용 대상:SQL ServerAzure SQL DatabaseAzure SQL Managed Instance

다음 쿼리는 문서 구조를 설명하는 XML-DATA 스키마를 반환합니다.

USE AdventureWorks2022;
GO
SELECT ProductModelID, Name
FROM Production.ProductModel
WHERE ProductModelID IN (122, 119)
FOR XML RAW, XMLDATA;
GO

결과는 다음과 같습니다.

<Schema name="Schema1" xmlns="urn:schemas-microsoft-com:xml-data"
        xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <ElementType name="row" content="empty" model="closed">
    <AttributeType name="ProductModelID" dt:type="i4" />
    <AttributeType name="Name" dt:type="string" />
    <attribute type="ProductModelID" />
    <attribute type="Name" />
  </ElementType>
</Schema>
<row xmlns="x-schema:#Schema1" ProductModelID="122" Name="All-Purpose Bike Stand" />
<row xmlns="x-schema:#Schema1" ProductModelID="119" Name="Bike Wash" />

참고 항목

<Schema> 임스페이스로 선언됩니다. 서로 다른 FOR XML 쿼리에서 여러 XML-Data 스키마를 요청할 때는 네임스페이스 충돌을 피하기 위해 쿼리가 실행될 때마다 네임스페이스 식별자(이 예제에서는 Schema1 )가 변경됩니다. 네임스페이스 식별자는 n이 정수인 스키마n으로 구성됩니다.

옵션을 지정하여 XMLSCHEMA 결과에 대한 XSD 스키마를 요청할 수 있습니다.

USE AdventureWorks2022;
GO
SELECT ProductModelID, Name
FROM Production.ProductModel
WHERE ProductModelID IN (122, 119)
FOR XML RAW, XMLSCHEMA;
GO

결과는 다음과 같습니다.

<xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="https://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified">
  <xsd:import namespace="https://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="https://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
  <xsd:element name="row">
    <xsd:complexType>
      <xsd:attribute name="ProductModelID" type="sqltypes:int" use="required" />
      <xsd:attribute name="Name" use="required">
        <xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks].[dbo].[Name]">
          <xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
            <xsd:maxLength value="50" />
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:attribute>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>
<row xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1" ProductModelID="122" Name="All-Purpose Bike Stand" />
<row xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1" ProductModelID="119" Name="Bike Wash" />

대상 네임스페이스 URI를 FOR XML의 XMLSCHEMA에 대한 선택적 인수로 지정할 수 있습니다. 스키마에서 지정된 대상 네임스페이스를 반환합니다. 이 대상 네임스페이스는 쿼리를 실행할 때마다 동일하게 유지됩니다. 예를 들어 이전 쿼리의 다음 수정된 버전에는 네임스페이스 URI 'urn:example.com'가 인수로 포함되어 있습니다.

USE AdventureWorks2022;
GO
SELECT ProductModelID, Name
FROM Production.ProductModel
WHERE ProductModelID IN (122, 119)
FOR XML RAW, XMLSCHEMA ('urn:example.com');
GO

결과는 다음과 같습니다.

<xsd:schema targetNamespace="urn:example.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="https://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified">
  <xsd:import namespace="https://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="https://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
  <xsd:element name="row">
    <xsd:complexType>
      <xsd:attribute name="ProductModelID" type="sqltypes:int" use="required" />
      <xsd:attribute name="Name" use="required">
        <xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks].[dbo].[Name]">
          <xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
            <xsd:maxLength value="50" />
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:attribute>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>
<row xmlns="urn:example.com" ProductModelID="122" Name="All-Purpose Bike Stand" />
<row xmlns="urn:example.com" ProductModelID="119" Name="Bike Wash" />

참고 항목