2017-04-17 56 views
0

我有一個要求以XML格式獲取表格的模式。使用表格列名稱作爲XML標記在SQL中構建XML

考慮與列的表人:

ID VARCHAR(10)身份,名稱爲varchar(20),指定VARCHAR(10)

我需要的XML是格式

<Persons> 
<Person LocalizationSetting = {today's date}> 
<ID type=varchar length =10 required=true/> 
<Nametype=varchar length =20 required=true/> 
<Designation type=varchar length =10 required=true/> 
</Person> 
</Persons> 

我有以下腳本

DECLARE @TableSchema XML 

SELECT @TableSchema = (
    select column_name, 
      data_type as [type], 
      CHARACTER_MAXIMUM_LENGTH AS maxLength, 
      case(is_nullable) 
       when 'YES' then 'false' 
       else 'true'  
      end as [required] 
    from information_schema.columns [column] 
    where table_name = 'Person' 
    for xml auto,root('Persons') 
    ) 

SELECT @TableSchema 

我得到以下結果:

<Patients> 
    <column column_name="ID" type="varchar" maxLength="10" required="true" /> 
    <column column_name="Name" type="varchar" maxLength="20" required="true" /> 
    <column column_name="Designation" type="varchar" maxLength="10" required="true" /> 
</Patients> 

是否可以將column_name作爲標記顯示在預期結果中?

回答

1

你應該解決這個...

我有一個要求得到一個表的架構以XML格式。

...與標準內置功能。您提供的預期輸出的模式是一些自定義的結構可能...檢查了這一點:

CREATE TABLE Person (ID VARCHAR(10) NOT NULL 
        ,NameType VARCHAR(20) NOT NULL 
        ,Designation VARCHAR(10) NOT NULL); 
INSERT INTO Person VALUES('SomeID','SomeType','SomeDes'); 

SELECT * FROM Person FOR XML AUTO,XMLDATA; 

--XMLDATA返回此架構

<Schema name="Schema1" xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes"> 
    <ElementType name="Person" content="empty" model="closed"> 
    <AttributeType name="ID" dt:type="string" /> 
    <AttributeType name="NameType" dt:type="string" /> 
    <AttributeType name="Designation" dt:type="string" /> 
    <attribute type="ID" /> 
    <attribute type="NameType" /> 
    <attribute type="Designation" /> 
    </ElementType> 
</Schema> 

--The未來可能性

SELECT * FROM Person FOR XML AUTO,XMLSCHEMA; 

--XMLSCHEMA返回此架構

<xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified"> 
    <xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" /> 
    <xsd:element name="Person"> 
    <xsd:complexType> 
     <xsd:attribute name="ID" use="required"> 
     <xsd:simpleType> 
      <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth"> 
      <xsd:maxLength value="10" /> 
      </xsd:restriction> 
     </xsd:simpleType> 
     </xsd:attribute> 
     <xsd:attribute name="NameType" use="required"> 
     <xsd:simpleType> 
      <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth"> 
      <xsd:maxLength value="20" /> 
      </xsd:restriction> 
     </xsd:simpleType> 
     </xsd:attribute> 
     <xsd:attribute name="Designation" use="required"> 
     <xsd:simpleType> 
      <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth"> 
      <xsd:maxLength value="10" /> 
      </xsd:restriction> 
     </xsd:simpleType> 
     </xsd:attribute> 
    </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

如果您確實需要滿足您遇到問題的預期格式,那麼本機查詢不支持動態列名稱。

WITH AlmostCorrect AS 
(
SELECT 
    (
    SELECT GETDATE() AS [@LocalizationSetting] 
      ,(
      SELECT COLUMN_NAME AS [@name] 
        ,DATA_TYPE AS [@type] 
        ,CHARACTER_MAXIMUM_LENGTH AS [@length] 
        ,CASE IS_NULLABLE 
         WHEN 'YES' THEN 'false' 
         ELSE 'true'  
        END AS [@required] 
      FROM INFORMATION_SCHEMA.COLUMNS 
      WHERE TABLE_NAME = 'Person' 
      FOR XML PATH('row'),TYPE 
      ) AS [*] 
    FOR XML PATH('Person'),ROOT('Persons'),TYPE 
    ) AS TheXML 
) 
SELECT CAST(REPLACE(REPLACE(CAST(TheXML AS NVARCHAR(MAX)),'<row name="','<'),'" type',' type') AS XML) 
FROM AlmostCorrect 

的:作爲一種替代方法,你可能會在最後的,動態創建SQL與EXEC或這種方法,它是利用在串級一個非常醜陋的黑客運行(東西一個,而應該用XML避免!)結果

<Persons> 
    <Person LocalizationSetting="2017-04-18T11:05:30.047"> 
    <ID type="varchar" length="10" required="true" /> 
    <NameType type="varchar" length="20" required="true" /> 
    <Designation type="varchar" length="10" required="true" /> 
    </Person> 
</Persons> 
+0

感謝您的詳細回覆。它完美的作品。 – perplexedDev

相關問題