You are on page 1of 40

DAT241: XML In SQL Server 2000 - New

Features And Enhancements


Cris Brown
Microsoft Corporation
Michael Brundage
Microsoft Corporation
Agenda
 SQL-2000 FOR XML Functionality
 Performance Considerations
 HTTP Access to Data
 Schemas
 Templates
 OpenXML, Updategrams, XML BulkLoad
 Roadmap to W.R.3

Prerequisites
XML Basics
SQL Server Tools
Transact-SQL (T-SQL)
XML For SQL Server Technology Road Map

SQL Server 2000


(FOR XML query
extension)

HTTP Access,
Templates

XML Views, XPath,


Updategrams, XML Bulkload
XML For SQL Server Architecture

Browsers
I
S SQL
SQLXML
IIS A (XML View)
Server
Applications P 2000
I

Access through ADO


DB DB

Client Middle Tier SQL Server


For XML Query Results
 SQL Language Extension
SELECT…
FROM…
WHERE…
ORDER BY…
FOR XML (raw |
auto [, ELEMENTS] |
explicit)
[, XMLData]
[, BINARY base64])
Using For..XML
 By default SQL returns XML fragments, not well-
formed XML
 For RAW … each row brought back as a set of
attributes around an XML element row
 For Auto… (More Control) By default each row is
an attribute based around the element tablename
 For Auto… use the ELEMENTS clause to return
columns as sub-elements
 Order of your Elements / Sub-Elements depends on
the SELECT clause
 EXPLICIT mode – steep curve to code, WR1
introduced SCHEMA’s – check, you may not need
to use explicit
 Binary64 mode – for large text values or BLOB’s
(else get url pointers)
FOR XML
Using For…XML
 Use Aliases to get around problem of special
encoding parameters & change attribute
names

SELECT OrderID InvoiceNo,


ProductID, UnitPrice Price, Quantity
FROM [Order Details] Item
WHERE OrderID = 10248 FOR XML AUTO
Without : <Order_x0020_Details …> ?
With :
<Item InvoiceNo="10248" ProductID="11" Price="14" …
<Item InvoiceNo="10248" ProductID="42" Price="9.8" …
<Item InvoiceNo="10248" ProductID="72" Price="34.8" …
For XML Query Results
 SQL Language Extension
SELECT…
FROM…
WHERE…
ORDER BY…
FOR XML (raw |
auto [, ELEMENTS] |
explicit)
[, XMLData]
[, BINARY base64])
Which is best for performance?
FOR XML AUTO
Which is best for performance?
FOR XML EXPLICIT
Which is best for performance?
FOR XML AUTO Win’s here!
(consistently less CPU Time)

 The Sort in the EXPLICIT takes longer


than the Join in the AUTO
 Not always true .. If the Join didn’t have
indexes, Join might be more expensive
 In most cases, FOR XML AUTO Win’s
 RAW slightly faster (less formatting)
 Over-riding key consideration :
speeding up the SQL query has biggest
effect on performance (indexing
strategies, query plans etc.)
Retrieving Data via HTTP
Virtual Directory

 Required for HTTP access


 Virtual Names can be set by the SQL IIS
Admin utility or programatically
 Virtual Names require a name, a path to
directory or file, and a type
 Supports Dbobject, Schema, or
Template types
Virtual Directory
Management Using WR2
URL Queries
 A simple access method to issue any
valid TSQL command via an HTTP
request.
 May display in IE or not depending on the
command’s output (is it well-formed XML?)
 Uses the dbobject virtual directory type

WARNING!
Not recommended for
Production Systems,
Switched OFF as default
Retrieving Data
Annotated Schemas

 Mapping Schema provide an XML view


of relational database tables.
 Defines the relationship between
different elements within database
and the XML document
 Were created due to avoid the
complexity of using Explicit
mode queries.
 Currently does not provide support for
some relationships, thus an Explicit
mode query must be used.
Retrieving Data
Annotated Schemas
Sql:relation DB Table for ElementType entries
Sql:relationship Defines relationships within
schema between two ElementType
entries
Sql:field Specifies DB column name
Sql:key-fields Key for the DB table multiple keys
list in order from Major -> Minor

sql:relationship
key-relation Primary Db table in join
key Key field for join
foreign-relation Foreign Db table in join
foreign-key Foreign key field for join
Retrieving Data
Annotated Schemas

<ElementType name="Order" sql:relation="Orders" sql:key-fields="OrderID">


<AttributeType name="CustomerID" />
<AttributeType name="OrderID" />
<attribute type="CustomerID" /> XML Name
<attribute type="OrderID" /> DB Name
</ElementType>
<ElementType name="Customer" sql:relation="Customers"
sql:key-fields="CustomerID">
<AttributeType name="UniqueCustomerNumber"/>
<attribute type="UniqueCustomerNumber" sql:field="CustomerID"/>
<element type="Order" >
<sql:relationship key-relation="Customers"
key="CustomerID"
foreign-relation="Orders"
foreign-key="CustomerID" />
</element>
</ElementType>
Schemas
Templates
 May contain multiple queries/commands
and/or Mapping schemas as needed.
 An XML file formatted which SQL 2000
understands
 Provides an abstraction layer between the
consumer and the database
 May contain one (and only one) XSL file per
query
 Supports parameters and stored procedures
(use SP’s! .. avoid dym sql)
 They are cached on server (more later)
Top Tips when using Templates
 Think as an a SP or COM Object .. An
abstraction
 Supports both SQL Select or XPath
queries
 Must use MDAC/ADO 2.6 or higher
 Expects everything in Templates
directory to be templates or XSL files
 Recommended for most applications
Retrieving Data
Templates

<?xml version='1.0' ?>


<root xmlns:sql="urn:schemas-microsoft-
com:xml-sql" sql:xsl="products.xsl" >
<sql:query> XPATH is
SELECT * CaSE SEnSiTIvE
MuST MATch CaSe!!!
FROM Products
ORDER BY ProductName
FOR XML AUTO
</sql:query>
</root>
Templates

Server 2
Performance addition in WR2
 NEW! in WR2 :

Client-Side Generating of XML


 Good for scale-out of middle-tier
performance
 Can use against SQL-Server 7
Modifying Data
 OpenXML
 used to modify data by loading XML from
within the SQL environment using T-SQL
 UpdateGrams
 used to modify SQL data from within an
XML document
 BulkLoad
 Provides high performance when
inserting large amounts of XML
data into SQL server
OpenXML
XML DOM

sp_xml_preparedocument
XML Parse


OpenXML
shred
… …

name ID Phone
SQL Server 2000
insert into …


Modifying Data
OpenXML Clause

 OpenXML Is Combined with


T-SQL Code
 Use OpenXML as a relational view of XML
 Execute any valid operation – read/write

INSERT INTO Customers(CustID, FirstName)


SELECT *
FROM OpenXML(@idoc, 'Customer‘, 1)
WITH (CustID varchar(5),
FirstName nvarchar(30))
OPENXML

Laptop
Modifying Data
UpdateGrams

 Modify the database through an


XML View
 Declare before and after images of the
XML (what it is now, and what you
want it to be)
 Each updategram is translated into one
or more INSERT, UPDATE, and DELETE
statements, carried out as a single
transaction
 Shares parameters with XPath and SQL
Modifying Data
UpdateGrams

<sql:update
xmlns:sql="urn:schemas-microsoft-com:xml-
updategram“>
<sql:sync mapping-schema=“nwind.xml”>
<sql:before>
<Customer CustomerID=“LAZYK”
CompName=“Lazy K City Market”>
</Customer>
</sql:before>
<sql:after>
<Customer CustomerID=“LAZYK”
CompName=“Lazy K Country Store”>
</Customer >
</sql:after>
</sql:sync>
</sql:update>
UpdateGrams

Server 2
Modifying Data
BulkLoad

 Similar to SQL BulkLoad, with support


for hierarchical relationships and
semi-structured data.
 Performance is about 75% of
traditional bulkload.
 Scriptable COM object.
 Requires a mapping (annotated XDR)
schema
 Can use or ignore database constraints
 Transacted or non-Transacted modes
XML Bulkload And XML Views
Sample VB Script:
set objBL =
CreateObject("SQLXMLBulkLoad.SQLXMLBulkLo
ad")
objBL.ConnectionString = objConn
objBL.ErrorLogFile = "error.xml"
objBL.XMLFragment = True
objBL.CheckConstraints = False
objBL.Execute "schema.xsd", "data.xml"
Set objBL = Nothing
XML For SQL-Server Release
Schedule
XML for SQL Server –
Web Releases
 WR1 was fully backwards compatible
with SQL Server 2000 release.
 WR2 will not be fully 100% backwards
compatible – so must test applications
before putting into production.
 Going forward, will be releasing in the
side - by - side model.
XML For SQL Server –
Web Release Schedule
 Web Release 2 (WR2)
 Annotated XSD support, client side For
XML, SQLXML OLEDB provider,
Integration with ADO.net.
 Beta 2 – July 2001
 RTM – September 2001
 Web Release 3 (WR3)
 XQuery, Heterogeneous query support.
 Beta – Late Fall 2001
 RTM – Early Spring 2002
Call To Action
 You can find the newest XML for SQL Server
releases at:
 http://msdn.microsoft.com/sqlserver

 Feedback, Feedback, Feedback – We want it,


we need it!
 PSS, MCS, etc.
 xmlsqlfb@microsoft.com
 news:microsoft.public.sqlserver.xml
More Sessions
 WEB328 – Thursday Afternoon
(SQL XML Overview)
 WEB331 – Friday Morning
(Drill-down on XML View)

Books
 Professional XML Databases by WROX
 Programming SQL-2000 with XML
Resources
 http://msdn.microsoft.com/xml/
 http://msdn.microsoft.com/sqlserver
 http://www.microsoft.com/sql/
 http://www.w3.org/XML/

Thanks for your time!

You might also like