Exforsys

SQL Server 2005 Training

  1. SQL Server 2005 - Configuring Replication
  2. SQL Server 2005 Replication Enhancements
  3. SQL Server 2005 - Mirror Server
  4. SQL Server 2005 - Introduction to Data Availability
  5. SQL Server 2005 - Backing up a Database
  6. SQL Server 2005 - Using Database Snapshots
  7. SQL Server 2005 - Disaster Recovery
  8. SQL Server 2005 - Managing Certificates
  9. SQL Server 2005 - Managing Permissions
  10. Managing SQL Server 2005 Security
  11. SQL Server 2005 - Using the Database Tuning Advisor
  12. SQL Server 2005 - Tuning a Database
  13. Maintain indexes in a SQL Server 2005 database
  14. SQL Server 2005 - Defining Indexes
  15. SQL Server 2005 - Database Backup
  16. SQL Server 2005 - Populating the Database
  17. SQL Server 2005 Configuration Manager
  18. SQL Server 2005 - Using the Sqlcmd Utility
  19. Using the SQL Management Objects
  20. Using SQL Sever Management Studio - Part 2
  21. Using SQL Sever Management Studio - Part 1
  22. SQL Server 2005 - Using Event Notifications
  23. SQL Server 2005 - Using DDL Triggers
  24. SQL Server Monitoring Tools - Server Profiler
  25. SQL Server 2005 - Testing Troubleshooting
  26. SQL Server 2005 - Upgrading from earlier versions of SQL Server
  27. SQL Server 2005 Installation - Maintenance Plan Without Using Wizard
  28. SQL Server 2005 - Unattended Installations
  29. SQL Server 2005 Installation - Maintenance Plan Using Wizard
  30. Installing a Second Copy of SQL Server 2005
  31. Planning to Install SQL Server 2005
  32. SQL Server 2005 Installation
  33. SQL server 2005 Editions
  34. SQL Server 2005 Architecture Overview
  35. SQL Server 2005 - Management studio interface Summary Page
  36. SQL Server 2005 - Server Groups
  37. SQL Server 2005 - Registered Servers
  38. SQL Server 2005 Administrative Tools
  39. Developing Client applications in SQL Server 2005
  40. SQL Server Management Objects
  41. NET CLR in SQL Server 2005
  42. Native HTTP Support in SQL Server 2005
  43. XML Data Types in SQL Server 2005
  44. Using XML in SQL Server 2005
  45. Using Notification Services in SQL Server 2005
  46. SQL Server 2005 - Service Broker
  47. Data Manipulation Language (DML) in SQL Server 2005
  48. T-SQL Enhancements in SQL Server 2005
  49. Security Features in SQL Server 2005 for the Developer
  50. SQL Server Architecture and Components
  51. SQL Server 2005 Management Studio
  52. Overview of SQL Server 2005 for the Database Developer
  53. Getting started with SQL Server 2005

Ads


Home arrow Technical Training arrow SQL Server 2005 Training

Using XML in SQL Server 2005

Author : Exforsys Inc.     Published on: 26th Oct 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 >

Ads

The improvements made to SQL Server 2005 really enhance user experience and resolves many of the issues which required complicated coding by developers.



 
This tutorial is part of a SQL Server 2005 Training tutorial series. Read it from the beginning and learn yourself.

SQL Server 2005 Training

  1. SQL Server 2005 - Configuring Replication
  2. SQL Server 2005 Replication Enhancements
  3. SQL Server 2005 - Mirror Server
  4. SQL Server 2005 - Introduction to Data Availability
  5. SQL Server 2005 - Backing up a Database
  6. SQL Server 2005 - Using Database Snapshots
  7. SQL Server 2005 - Disaster Recovery
  8. SQL Server 2005 - Managing Certificates
  9. SQL Server 2005 - Managing Permissions
  10. Managing SQL Server 2005 Security
  11. SQL Server 2005 - Using the Database Tuning Advisor
  12. SQL Server 2005 - Tuning a Database
  13. Maintain indexes in a SQL Server 2005 database
  14. SQL Server 2005 - Defining Indexes
  15. SQL Server 2005 - Database Backup
  16. SQL Server 2005 - Populating the Database
  17. SQL Server 2005 Configuration Manager
  18. SQL Server 2005 - Using the Sqlcmd Utility
  19. Using the SQL Management Objects
  20. Using SQL Sever Management Studio - Part 2
  21. Using SQL Sever Management Studio - Part 1
  22. SQL Server 2005 - Using Event Notifications
  23. SQL Server 2005 - Using DDL Triggers
  24. SQL Server Monitoring Tools - Server Profiler
  25. SQL Server 2005 - Testing Troubleshooting
  26. SQL Server 2005 - Upgrading from earlier versions of SQL Server
  27. SQL Server 2005 Installation - Maintenance Plan Without Using Wizard
  28. SQL Server 2005 - Unattended Installations
  29. SQL Server 2005 Installation - Maintenance Plan Using Wizard
  30. Installing a Second Copy of SQL Server 2005
  31. Planning to Install SQL Server 2005
  32. SQL Server 2005 Installation
  33. SQL server 2005 Editions
  34. SQL Server 2005 Architecture Overview
  35. SQL Server 2005 - Management studio interface Summary Page
  36. SQL Server 2005 - Server Groups
  37. SQL Server 2005 - Registered Servers
  38. SQL Server 2005 Administrative Tools
  39. Developing Client applications in SQL Server 2005
  40. SQL Server Management Objects
  41. NET CLR in SQL Server 2005
  42. Native HTTP Support in SQL Server 2005
  43. XML Data Types in SQL Server 2005
  44. Using XML in SQL Server 2005
  45. Using Notification Services in SQL Server 2005
  46. SQL Server 2005 - Service Broker
  47. Data Manipulation Language (DML) in SQL Server 2005
  48. T-SQL Enhancements in SQL Server 2005
  49. Security Features in SQL Server 2005 for the Developer
  50. SQL Server Architecture and Components
  51. SQL Server 2005 Management Studio
  52. Overview of SQL Server 2005 for the Database Developer
  53. Getting started with SQL Server 2005
 

Comments