Technical Training
SQL Server 2005 TrainingUsing XML in SQL Server 2005
Using XML in SQL Server 2005
In this tutorial you will learn about using XML in SQL Server 2005 - XML enhancements in SQL Server 2005, the FOR Clause, Using XSINIL with ELEMENTS, Using elementsxinil in EXPLICIT mode and Enhancements to OpenXML function
Ads
XML enhancements in SQL Server 2005
Microsoft SQL Server 2000 introduced XML capabilities to the server in a very limited manner. Developers could export relational data as XML and shred the same back into XML. However, such data could not be stored in the database, except as large string values—the data of which could not be manipulated without string manipulation functions.
Microsoft SQL Server 2005 introduces the XML data type into the database engine. This can be queried, indexed and manipulated like any other data type. The user has a choice of storing the data as XML data type or in columns for frequent manipulation. If the data is stored in columns, the developer has to ensure that the data can be converted into XML and sent or received as such and reconverted for storage. However, this becomes essential only if the data is frequently manipulated and also sent and received across the network. If the data is not frequently manipulated, it makes sense to store it as XML data type in the database, as XML data type also provides for query on data, access to individual data and check for existence of data etc.
Two major enhancements were made to the XML features that existed in SQL Server 2000 to enable the transformation of relational data to and from XML as needed. The first of these is the FOR clause and the second is the OPENXML clause.
The FOR Clause
The FOR clause allows developers to transform relational data into well formed XML. The enhancements are as under:
1. In SQL Server 2000 the RAW mode for XML queries were designed to return only attribute centric XML. SQL Server 2005 uses the ELEMNTS directive along with the RAW mode queries for element centric XML. The syntax for the same would be as under:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SELECT CourseID, Name and CoursePrice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FROM CourseMaster.Course ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FOR XML RAW, ELEMENTS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;2. SQL Server 2005 allows developers to specify that null columns should yield empty elements with the xsi:nil attribute instead of being omitted. The XSINIL option can be used with ELEMENTS directive in AUTO, RAW and PATH mode queries. Alternately, the elementxsinil column mode can be used in EXPLICIT mode queries. See the examples below for the usage.
Using XSINIL with ELEMENTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SELECT CourseId, Name, description ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FROM CourseMaster.Course ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FOR XML AUTO, ELEMENTS XSINIL;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Using elementsxinil in EXPLICIT mode
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SELECT 1 AS Tag, ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
NULL AS Parent, ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CourseID AS [Course!1!CourseID], ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Name AS [Course!1!CourseName!element], ;;;;;;;;;;;;;;;;;;;;;;;;;;;
Color AS [Course!1!Description!elementxsinil] ;;;;;;;;;;;;;;;;;;;;
FROM CourseMaster.Course ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FOR XML EXPLICIT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;3. Unlike SQL Server 2000 which returns an inline XDR schema, SQL Server 2005, adds the XMLSCHEMA directive to return the XSD schema.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Select CourseID, Name, CoursePrice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FROM CouseMaster.Course Course ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FOR XML AUTO, XMLSCHEMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4. The new datatype XML, can be used with the TYPE directive in the FOR XML query to return the results as xml value instead of varchar string. FOR XML can be nested in queries to return multilevel XML results in AUTO and RAW mode queries.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SELECT CourseId, Name, CoursePrice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(SELECT ExaminerName, comments ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FROM CourseMaster.CourseExamination CourseExamination ;;;;;;;;;;;;
WHERE CourseExamination.CourseID= Course.CourseID ;;;;;;;;;;;;;;;;
FOR XML AUTO,ELEMENTS, TYPE) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FROM CourseMaster.Course Course ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FOR XML AUTO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;5. The PATH mode in FOR XML queries introduced by SQL Server 2005 can be used to specify column names using the XPath like syntax to map values to attributes, subelements, text nodes and data values.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SELECT CourseId AS “@CourseID”, ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Name AS “*”, ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Description AS “Description” ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FROM CourseMaster.Course ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FOR XML PATH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6. By default the FOR XML queries return XML fragments. The FOR XML query fragment returned will have to be wrapped in a root element to make it well formed as under:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SELECT CourseID, Name, CoursePrice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FROM CourseMaster.Course Course ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FOR XML AUTO, ROOT (‘Course Details’) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7. It is possible to specify alternate element name for each row element retrieved by a RAW or PATH mode.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SELECT CourseID, Name, CoursePrice ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FROM CourseMaster.Course ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FOR XML RAW (‘PRODUCT’);;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Enhancements to OpenXML function
The OPENXML function allows developers to parse XML data so that it can be stored as relational data in tabular form. This function supports the XML data type and the sp_xml_preparedocument system stored procedure accepts this new data type. This procedure is used by OPENXML function.
Whereas SQL Server 2000 allowed the use of varchar, nvarchar, text or ntext variables to generate a document handle using the abovementioned stored procedure, SQL Server 2005 allows developer to use the xml variable additionally.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DECLARE @doc xml ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SET @doc = '< ?xml version="1.0" ?> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
< SalesInvoice InvoiceID="1000" StudentID="123" > ;;;;;;;;;;;;;;;;;;;;;;
< Items > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
< Item CourseCode="12" CourseUnitPrice="12.99" / > ;;;;;;;;;;;;;;;;;;;;;
< Item CourseCode="41" CourseUnitPrice="17.45" / > ;;;;;;;;;;;;;;;;;;;;;
< Item CourseCode="2" CourseUnitPrice="2.99" / > ;;;;;;;;;;;;;;;;;;;;;;;
< / Items > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
< / SalesInvoice > ' ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DECLARE @docHandle int ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
EXEC sp_xml_preparedocument @docHandle OUTPUT, @doc ;;;;;;;;;;;;;;;;;;;;
SELECT * FROM ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
OPENXML(@docHandle, 'SalesInvoice/Items/Item', 1) ;;;;;;;;;;;;;;;;;;;;;;
WITH ;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;
(CourseCode int, ;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;
UnitPrice smallmoney) ;;;;;; ;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;
EXEC sp_xml_removedocument @docHandle;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
The xml data type columns are returned if the WITH clause is used in the OPENXML function.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SELECT * FROM ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
OPENXML (@docHandle, ‘SalesInvoice’, 1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
WITH ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(InvoiceID int, ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
StudentID int, ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
OrderDate datetime, ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Itmes xml ‘Ítems’);;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
It must be noted that the document handle is destroyed at the end of a batch to reduce the impact on the resources in SQL Server 2005, unlike in SQL Server 2000 where the handle is retained for the duration of the session. The syntax will be as under:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DECLARE @doc xml ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SET @doc= ‘< ? xml version= “1.0”? > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
< SalesInvoice InvoiceID=“1000” StudentID=“123” OrderDate= “2004-03-07”>
< Items > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
< Item CourseCode= “12” CourseUnitPrice= “12.99” / > ;;;;;;;;;;;;;;;;;;;
< Item CourseCode= “14” CourseUnitPrice= “17.45” / > ;;;;;;;;;;;;;;;;;;;
< / Items > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
< / SalesInvoice > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DECLARE @docHandle int ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
EXEC spe_xml_preparedocument @docHandle OUTPUT, @doc ;;;;;;;;;;;;;;;;;;;
SELECT * FROM ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
OPENXML (@docHandle, ‘SalesInvoice’,1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
WITH ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
InvoiceID int, ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
StudentID int, ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
OrderDate datetime, ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Items xml ‘Items”);;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Note that the results column contains xml data.
|
InvoiceID |
StudentID |
OrderDate |
Items |
|
1000 |
123 |
2004-03-07 00:00:00.000 |
< Items > < Item CourseCode="12" CourseUnitPrice="12.99" / > < Item CourseCode="14" CourseUnitPrice="17.45" / > < / Items > |
The improvements made to SQL Server 2005 really enhance user experience and resolves many of the issues which required complicated coding by developers.
SQL Server 2005 Training
- SQL Server 2005 - Configuring Replication
- SQL Server 2005 Replication Enhancements
- SQL Server 2005 - Mirror Server
- SQL Server 2005 - Introduction to Data Availability
- SQL Server 2005 - Backing up a Database
- SQL Server 2005 - Using Database Snapshots
- SQL Server 2005 - Disaster Recovery
- SQL Server 2005 - Managing Certificates
- SQL Server 2005 - Managing Permissions
- Managing SQL Server 2005 Security
- SQL Server 2005 - Using the Database Tuning Advisor
- SQL Server 2005 - Tuning a Database
- Maintain indexes in a SQL Server 2005 database
- SQL Server 2005 - Defining Indexes
- SQL Server 2005 - Database Backup
- SQL Server 2005 - Populating the Database
- SQL Server 2005 Configuration Manager
- SQL Server 2005 - Using the Sqlcmd Utility
- Using the SQL Management Objects
- Using SQL Sever Management Studio - Part 2
- Using SQL Sever Management Studio - Part 1
- SQL Server 2005 - Using Event Notifications
- SQL Server 2005 - Using DDL Triggers
- SQL Server Monitoring Tools - Server Profiler
- SQL Server 2005 - Testing Troubleshooting
- SQL Server 2005 - Upgrading from earlier versions of SQL Server
- SQL Server 2005 Installation - Maintenance Plan Without Using Wizard
- SQL Server 2005 - Unattended Installations
- SQL Server 2005 Installation - Maintenance Plan Using Wizard
- Installing a Second Copy of SQL Server 2005
- Planning to Install SQL Server 2005
- SQL Server 2005 Installation
- SQL server 2005 Editions
- SQL Server 2005 Architecture Overview
- SQL Server 2005 - Management studio interface Summary Page
- SQL Server 2005 - Server Groups
- SQL Server 2005 - Registered Servers
- SQL Server 2005 Administrative Tools
- Developing Client applications in SQL Server 2005
- SQL Server Management Objects
- NET CLR in SQL Server 2005
- Native HTTP Support in SQL Server 2005
- XML Data Types in SQL Server 2005
- Using XML in SQL Server 2005
- Using Notification Services in SQL Server 2005
- SQL Server 2005 - Service Broker
- Data Manipulation Language (DML) in SQL Server 2005
- T-SQL Enhancements in SQL Server 2005
- Security Features in SQL Server 2005 for the Developer
- SQL Server Architecture and Components
- SQL Server 2005 Management Studio
- Overview of SQL Server 2005 for the Database Developer
- Getting started with SQL Server 2005







