Vous êtes sur la page 1sur 2

Shree Rahul Education Societys (Regd.

SHREE L.R. TIWARI COLLEGE OF


ENGINEERING
Kanakia Park, Mira Road(E),Thane-401107,Maharastra.
(Approved

by AICTE & DTE, Maharashtra State & Affiliated to University of


Mumbai)
ISO 9001:2008 Certified. DTE Code No. 3423

DEPARTMENT OF COMPUTER ENGINEERING


AIM: Working of XML with SQL server and creation of XML Schema.
THEORY:

With the growing use of XML data, the need for the coexistence of relational data and
XML data is also growing. The classic approach of storing XML data as physical disk
files is unsuitable and tedious in many situations. This article will give you a jump
start in using the XML data type of SQL server and how XML data can be
manipulated with the help of new XML Data Modification Language (XML DML).
Adding an XML Column to a Table
Before you delve any further, create a table in an SQL Server database that contains a
column of type XML.
Storing XML Data in the Table
Insert into tablename values( write XML data);
Modifying Parts of the XML Data
The XML data type provides what is known as XML Data Modification Language
(XML DML). XML DML statements allow you to insert, update or delete data from
the XML fragment stored in the xml column.
Now, say you want to insert another <employee> element in addition to what you
originally inserted (with employee ID 1). To accomplish this, you will need to execute
the following XML DML statement:
Update tablename
Set columnname.modify(insert XML data after (/employee)[1])
The modify() method accepts a string that instructs whether the data is to be inserted,
updated, or deleted followed by the actual data. The after clause specifies the location
where the new node is to be inserted. Just like the after clause you can also use before,
as first and as last clauses.
Now, assume that you want to substitute the employeeid attribute of an <employee>
element with a value of 100.
Update tablename
Set columnname.modify(replace value of(/employee/employeeid)[1] with 100 )
To delete an <employee> node, you will need to use the delete statement:
Update tablename
Set columnname.modify(delete(/employee[@employeeid=1] )
Querying XML Data
In the preceding examples, you dealt with data insertion and modification operations.
Equally important are query operations. Just like the Modify() method, the XML data

Shree Rahul Education Societys (Regd.)

SHREE L.R. TIWARI COLLEGE OF


ENGINEERING
Kanakia Park, Mira Road(E),Thane-401107,Maharastra.
(Approved

by AICTE & DTE, Maharashtra State & Affiliated to University of


Mumbai)
ISO 9001:2008 Certified. DTE Code No. 3423

DEPARTMENT OF COMPUTER ENGINEERING

type has several methods that allow you to query data. Some of the commonly used
methods of the XML data type are listed below:
query(): Queries the XML data based on XQuery expression and fetches the results of
the query
value(): Queries the XML data and returns a scalar value as a result
exist(): Indicates whether a given XQuery expression returned any results
The query() method accepts an XQuery expression on the basis of which data is to be
filtered and returns the results as XML.
Select columnname.query(/child[@attribute=value]) as newcolname
From tablename
The value() method takes two parameters: the XQuery expression returns a scalar
value and data type of the returned scalar value.
Select columnname.value((/child/subchild)[1] , datatype) as newcolname
From tablename
The exist() method accepts an XQuery expression and returns a value indicating
whether any matching node was found. If the matching node is found, it returns 1;
otherwise, it returns 0. If the column contains NULL, the value() method also returns
NULL.
Select columnname.exist(/child[@attribute=value]) as newcolname
From tablename

XML SCHEMA:
XML markup was not validated against any XSD schema. In many cases, using XSD schema
can be advantageous. Some of the advantages include:
Schemas allow you to detect errors in the XML data early in the life cycle of the
application.
It allows you to validate the new XML data being inserted to the XML column.
It also can improve the performance of query execution.
To attach a schema to an XML column, first of all you need to create a Schema Collection.
Syntax:
Create XML schema collection schemaname as write XML schema

CONCLUSION: Thus we have implemented XML with SQL server and created XML
Schema.

Vous aimerez peut-être aussi