Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SQL Server 2008 R2 Announced, CTP Coming This SummerUsing one Parameter for Multiple Datasets in Reporting Services »
    comments

    You have a table and you would like to create a XSD schema based on that table. What is the easiest way to do that in SQL Server? The easiest way to do that would be to use FOR XML syntax with AUTO, ELEMENTS and XMLSCHEMA. If your table is named test and you want your schema to be named TestXsdSchema then you would do the following

    1. SELECT * FROM Test FOR XML AUTO, ELEMENTS, XMLSCHEMA('TestXsdSchema')

    Let's look at a complete example. First create the table below

    1. create table Test(id int identity,
    2. SomeName varchar(53) not null,
    3. SomeValue decimal(20,10) not null,
    4. SomeGuid uniqueidentifier not null default newsequentialid())

    Now execute the following block of code

    1. DECLARE @XsdSchema xml
    2. SET @XsdSchema = (SELECT * FROM Test FOR XML AUTO, ELEMENTS, XMLSCHEMA('TestXsdSchema'))
    3. SELECT @XsdSchema

    This is the schema that gets generated

    1. <xsd:schema targetNamespace="TestXsdSchema" elementFormDefault="qualified">
    2.  <xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd"/>
    3. <xsd:element name="Test">
    4. <xsd:complexType>
    5. <xsd:sequence>
    6.  <xsd:element name="id" type="sqltypes:int"/>
    7. <xsd:element name="SomeName">
    8. <xsd:simpleType>
    9. <xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
    10.  <xsd:maxLength value="53"/>
    11.  </xsd:restriction>
    12.  </xsd:simpleType>
    13.  </xsd:element>
    14. <xsd:element name="SomeValue">
    15. <xsd:simpleType>
    16. <xsd:restriction base="sqltypes:decimal">
    17.  <xsd:totalDigits value="20"/>
    18.  <xsd:fractionDigits value="10"/>
    19.  </xsd:restriction>
    20.  </xsd:simpleType>
    21.  </xsd:element>
    22.  <xsd:element name="SomeGuid" type="sqltypes:uniqueidentifier"/>
    23.  </xsd:sequence>
    24.  </xsd:complexType>
    25.  </xsd:element>
    26.  </xsd:schema>

    See, that was pretty simple wasn't it?





    *** If you have a SQL related question try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    4076 views
    Instapaper

    3 comments

    Comment from: maurice [Visitor] Email
    maurice Dear Denis, thanks for your example,

    I need some help! on how to export a table to an xml file(path) via an xsd collection ... could any body help how to write the tsql instruction ?
    07/02/11 @ 13:42
    Comment from: Mazhar Karimi [Visitor]
    Mazhar Karimi Nice! Helpful.

    Regards,

    Mazhar Karimi
    07/30/11 @ 00:35
    Comment from: TR [Visitor]
    TR Helpful. Thanks
    12/27/11 @ 05:28

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)