0% found this document useful (0 votes)
50 views5 pages

SQL Server XML Data Type

The document outlines 3 steps to use XML data type in SQL Server: 1) Create an XML schema, 2) Create a table with an XML datatype that references the schema, and 3) Insert XML data conforming to the schema into the table. The schema defines the structure of movie data including languages, countries, genres, directors, and other related elements.

Uploaded by

Girish PS
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views5 pages

SQL Server XML Data Type

The document outlines 3 steps to use XML data type in SQL Server: 1) Create an XML schema, 2) Create a table with an XML datatype that references the schema, and 3) Insert XML data conforming to the schema into the table. The schema defines the structure of movie data including languages, countries, genres, directors, and other related elements.

Uploaded by

Girish PS
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Steps to use xml data type in SQL Server.

1. Create XML schema as follows.

CREATE XML SCHEMA COLLECTION MovieSchema AS


N'
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Movie">
<xs:complexType>
<xs:sequence>
<xs:element name="Languages">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Language" type="xs:string" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Countries">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Country" type="xs:string" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Genres">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Genre" type="xs:string" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Directors">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Director" type="xs:string" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Producers">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Producer" type="xs:string" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Screenplays">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Screenplay" type="xs:string" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Writers">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Writer" type="xs:string" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Actors">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Actor" type="xs:string" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Cinematographers">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Cinematographer" type="xs:string" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Editings">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Editing" type="xs:string" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Studios">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Studio" type="xs:string" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Distributors">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Distributor" type="xs:string" />

</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Synopsis" type="xs:string" />

<xs:element name="MoreOnMovie" type="xs:string" />


<xs:element name="Comments">
<xs:complexType>
<xs:sequence>
<xs:element
maxOccurs="unbounded" name="Comment">
<xs:complexType>
<xs:simpleContent>

<xs:extension base="xs:string">

<xs:attribute name="user" type="xs:string" />

<xs:attribute name="date" type="xs:string" />

</xs:extension>

</xs:simpleContent>
</xs:complexType>
</xs:element>

</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
'
2. Create a table with XML datatype
CREATE TABLE XMLTable
(id INT, xDoc XML (MovieSchema))
GO

3. Insert XML data into the table

Insert into XMLTable


Values (1, '
<Movie>
<Languages>
<Language>english</Language>
<Language>hindi</Language>
</Languages>
<Countries>
<Country>America</Country>
<Country>India</Country>
</Countries>
<Genres>
<Genre>drama</Genre>
</Genres>
<Directors>
<Director>drama</Director>
</Directors>
<Producers>
<Producer>drama</Producer>
</Producers>
<Screenplays>
<Screenplay>drama</Screenplay>
</Screenplays>
<Writers>
<Writer>drama</Writer>
</Writers>
<Actors>
<Actor>drama</Actor>
</Actors>
<Cinematographers>
<Cinematographer>drama</Cinematographer>
</Cinematographers>
<Editings>
<Editing>drama</Editing>
</Editings>
<Studios>
<Studio>drama</Studio>
</Studios>
<Distributors>
<Distributor>drama</Distributor>
</Distributors>
<Synopsis>drama</Synopsis>
<MoreOnMovie>drama</MoreOnMovie>
<Comments>
<Comment user="girish" date="today">drama</Comment>
</Comments>
</Movie>
')

You might also like