Post on 23-Dec-2015
SQL Server 2000SQL Server 2000and XMLand XML
Erik VeermanErik Veerman
ConsultantConsultant
IntellinetIntellinetBusiness IntelligenceBusiness Intelligence
ObjectivesObjectives
Give an overview on the new XML Give an overview on the new XML capabilities in SQL Server 2000capabilities in SQL Server 2000
Demonstrate rather than presentDemonstrate rather than present Provide base knowledge for…Provide base knowledge for…
XML architecture decisionsXML architecture decisions Research starting pointsResearch starting points Pros and cons of parallel XML Pros and cons of parallel XML
technology implementationstechnology implementations Data interaction leveraging SQL’s Data interaction leveraging SQL’s
XML capabilitiesXML capabilities
AgendaAgenda
The ability to retrieve and write XML The ability to retrieve and write XML data: data: Retrieve XML data using the SELECT Retrieve XML data using the SELECT
statement and the FOR XML clause.statement and the FOR XML clause. Write XML data using OPENXML Write XML data using OPENXML
rowset provider.rowset provider. The ability to access SQL Server The ability to access SQL Server
using HTTP.using HTTP. IIS ConfigurationIIS Configuration Template QueriesTemplate Queries XSL Style sheetsXSL Style sheets
AgendaAgenda
Support for XDR schemas/XML Support for XDR schemas/XML ViewsViews XPath queries against these schemas.XPath queries against these schemas.
XML for SQL Web ReleaseXML for SQL Web Release Bulk LoadBulk Load UpdategramUpdategram
ADO programming methods ADO programming methods (Chris Hagen)(Chris Hagen)
XML in use (Chris Hagen)XML in use (Chris Hagen)
Agenda:Agenda:SQL Server 2000 XML SQL Server 2000 XML
SELECTSELECT
How has SQL Server extended How has SQL Server extended the SELECT paradigm to the SELECT paradigm to support XML?support XML?
SELECT Statement Syntax:SELECT Statement Syntax:
SELECT … … …SELECT … … …
FOR XML FOR XML { RAW | AUTO | EXPLICIT } { RAW | AUTO | EXPLICIT }
[, XMLDATA][, XMLDATA]
[, ELEMENTS][, ELEMENTS]
[, BINARY Base64][, BINARY Base64]
Simple XML Query:Simple XML Query:SELECTSELECT oh.CustomerID, oh.OrderId,oh.CustomerID, oh.OrderId,
od.ProductID, od.UnitPrice, od.Quantityod.ProductID, od.UnitPrice, od.Quantity
FROM Orders ohFROM Orders oh
INNER JOIN [Order Details] odINNER JOIN [Order Details] od
ON oh.orderid = od.orderidON oh.orderid = od.orderid
FOR XML {RAW | AUTO}FOR XML {RAW | AUTO}
FOR XML RAW FormatFOR XML RAW Format
Takes the query result and Takes the query result and transforms each row in the transforms each row in the result set into an XML element result set into an XML element with a generic identifier <row /> with a generic identifier <row /> as the element tag.as the element tag.
FOR XML AUTO FormatFOR XML AUTO FormatReturns query results in a simple, Returns query results in a simple, nested XML tree. Each table in the nested XML tree. Each table in the FROM clause for which at least one FROM clause for which at least one column is listed in the SELECT column is listed in the SELECT clause is represented as an XML clause is represented as an XML element. The columns listed in the element. The columns listed in the SELECT clause are mapped to the SELECT clause are mapped to the appropriate element attributes.appropriate element attributes.
FOR XML EXPLICIT FormatFOR XML EXPLICIT Format
Based on the concept of a Universal Based on the concept of a Universal Table containing all the information Table containing all the information about the resulting XML tree.about the resulting XML tree.
Contains Tag and Parent meta-dataContains Tag and Parent meta-data Tag: Tag number of current elementTag: Tag number of current element Parent: Tag number of parent elementParent: Tag number of parent element
Column names are XML Generic Column names are XML Generic identifiers.identifiers. Identifier!TagNumber!AttributeNameIdentifier!TagNumber!AttributeName
EXPLICIT Sample QueryEXPLICIT Sample QuerySELECT 1 AS Tag, NULL AS Parent,SELECT 1 AS Tag, NULL AS Parent,
oh.CustomerID [Order!1!CustomerID], oh.OrderId [Order!1!OrderId],oh.CustomerID [Order!1!CustomerID], oh.OrderId [Order!1!OrderId],
NULL [OrderDetail!2!ProductId], NULL [OrderDetail!2!UnitPrice],NULL [OrderDetail!2!ProductId], NULL [OrderDetail!2!UnitPrice],
NULL [OrderDetail!2!Quantity]NULL [OrderDetail!2!Quantity]
FROM Orders oh WHERE oh.CustomerId = ‘ANTON’FROM Orders oh WHERE oh.CustomerId = ‘ANTON’
UNION ALLUNION ALL
SELECT 2, 1,SELECT 2, 1,
oh.CustomerID, oh.OrderId, od.ProductID, od.UnitPrice, od.Quantityoh.CustomerID, oh.OrderId, od.ProductID, od.UnitPrice, od.Quantity
FROM Orders oh INNER JOIN [Order Details] odFROM Orders oh INNER JOIN [Order Details] od
ON oh.orderid = od.orderid WHERE CustomerId = ‘ANTON’ON oh.orderid = od.orderid WHERE CustomerId = ‘ANTON’
ORDER BYORDER BY
[Order!1!CustomerID], [Order!1!OrderId], [OrderDetail!2!ProductId][Order!1!CustomerID], [Order!1!OrderId], [OrderDetail!2!ProductId]
FOR XML EXPLICITFOR XML EXPLICIT
SELECT XML FormatsSELECT XML Formats
DEMODEMO
Agenda:Agenda:SQL Server 2000 SQL Server 2000
OpenXMLOpenXMLAllows one to parse and utilize Allows one to parse and utilize an XML documentan XML document
OpenXML ProcessOpenXML Process
Input an XML documentInput an XML document Process an internal representationProcess an internal representation
exec sp_xml_preparedocumentexec sp_xml_preparedocument Parse the XML documentParse the XML document
OpenXML Rowset functionOpenXML Rowset function Remove internal representation Remove internal representation
from memoryfrom memory EXEC sp_xml_removedocumentEXEC sp_xml_removedocument
OpenXML ProcessOpenXML Process
OpenXML SyntaxOpenXML Syntax
OPENXML(idoc int [in], OPENXML(idoc int [in], rowpattern nvarchar[in], rowpattern nvarchar[in], [flags byte[in]])[flags byte[in]])
[WITH (SchemaDeclaration [WITH (SchemaDeclaration | TableName)]| TableName)]
OpenXML ProcessOpenXML Process
DEMODEMO
Agenda:Agenda:SQL Server 2000 IIS SQL Server 2000 IIS
IntegrationIntegration
How does one use IIS for How does one use IIS for retrieving XML data.retrieving XML data.
Using IIS, the process.Using IIS, the process.
Configure an IIS Virtual RootConfigure an IIS Virtual Root Define template queriesDefine template queries Define style sheetsDefine style sheets Make HTTP requestMake HTTP request
Configure an IIS Virtual Configure an IIS Virtual RootRoot
DEMODEMO
HTTP Access to DataHTTP Access to Data
URL QueryURL Query http://sqlserver/vroot?parametershttp://sqlserver/vroot?parameters
ParametersParameters Sql = SELECT+*+FROM+Sku+FOR+ Sql = SELECT+*+FROM+Sku+FOR+
XML+RAWXML+RAW Encoding = UTF-8Encoding = UTF-8 Root = rootRoot = root
HTTP Access to DataHTTP Access to Data
Direct QueryDirect Query Good for a single select of a single Good for a single select of a single
columncolumn Value is returned in native format, not Value is returned in native format, not
XMLXML Enables direct retrieval of objects like Enables direct retrieval of objects like
images, OLE objects, etc.images, OLE objects, etc. http://sqlserver/vroot/dbobject/xpathhttp://sqlserver/vroot/dbobject/xpath
What are Template Queries?What are Template Queries?
These are XML documents that These are XML documents that define queriesdefine queries
Can be bound directly to Style Can be bound directly to Style SheetsSheets
Called by referencing the XML Called by referencing the XML document in the URLdocument in the URL
What are Template Queries?What are Template Queries?
TemplateTemplate http://sqlserver/vroot/vname?paramshttp://sqlserver/vroot/vname?params Vname is a complete path to the Vname is a complete path to the
template XML filetemplate XML file Parameters may be xsl, encoding, or Parameters may be xsl, encoding, or
contenttype; or user defined.contenttype; or user defined. Provides an easy way to perform Provides an easy way to perform
complex queries with little network complex queries with little network traffictraffic
Why use Template Queries?Why use Template Queries?
Can use dynamic SQL but no Can use dynamic SQL but no restrictions on what is executedrestrictions on what is executed
Automatic formatting of the XML Automatic formatting of the XML header informationheader information
Templates simplify the HTTP Templates simplify the HTTP requestrequest
Templates have better support Templates have better support parametersparameters
Define and Use StyleSheetsDefine and Use StyleSheets
Can be added to the URLCan be added to the URL XSL=stylesheet.xslXSL=stylesheet.xsl ContentType = text/htmlContentType = text/html
Can be defined in the TemplateCan be defined in the Template
What does a template What does a template query look like and how is query look like and how is
it run?it run?
DEMODEMO
Agenda:Agenda:XML XDR SchemaXML XDR Schema
XML Views | Update gramXML Views | Update gram
Mapping relational data through Mapping relational data through an XML schemaan XML schema
What is an XML View?What is an XML View? Defines an XML-formatted view on Defines an XML-formatted view on
the databasethe database Annotations specify the XML to Annotations specify the XML to
relational database mapping for relational database mapping for column values and relationshipscolumn values and relationships
Uses Xpath to query the XML ViewUses Xpath to query the XML View
How to use XML ViewsHow to use XML Views http://server/vroot/vname/xpath?http://server/vroot/vname/xpath?
parametersparameters Vname is a direct reference to the Vname is a direct reference to the
schema fileschema file Xpath is the xpath queryXpath is the xpath query Parameters may be xsl, encoding, Parameters may be xsl, encoding,
contenttype, or user definedcontenttype, or user defined
How do I select data from How do I select data from an XML View?an XML View?
DEMODEMO
XML for SQL Web ReleaseXML for SQL Web Release
XML for SQL Web Release 1XML for SQL Web Release 1 UpdateGramsUpdateGrams Bulk Load XML supportBulk Load XML support
XML for SQL Web Release 2 XML for SQL Web Release 2 (July 2001)(July 2001) XML Views with XSDXML Views with XSD Support for DiffGramsSupport for DiffGrams Client Side XML formattingClient Side XML formatting
Update gramUpdate gram
Schema mapping, Update gram syntaxSchema mapping, Update gram syntax Inserts, Updates, DeletesInserts, Updates, Deletes Using <Before> and <After> blocksUsing <Before> and <After> blocks Passing ParametersPassing Parameters
Writing Data withWriting Data withUpdate GramsUpdate Grams
Update grams (example)Update grams (example)<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-<ROOT xmlns:updg="urn:schemas-microsoft-com:xml-
updategram">updategram"><updg:sync mapping-schema="SampleSchema.xml" ><updg:sync mapping-schema="SampleSchema.xml" ><updg:before><updg:before></updg:before></updg:before><updg:after><updg:after> <Customer CustID="AAAAA" Company="Bottom-<Customer CustID="AAAAA" Company="Bottom-
Dollar Markets" />Dollar Markets" /> </updg:after></updg:after></updg:sync></updg:sync></ROOT></ROOT>
Miscellaneous Data Retreival:Miscellaneous Data Retreival:OLAP dataOLAP data
sp_makewebsqlsp_makewebsql
DEMODEMO
Agenda:Agenda:ADO Programming ADO Programming
Methods for SQL Server Methods for SQL Server 2000 XML2000 XML
How does one write How does one write components utilizing the new components utilizing the new SELECT FOR XML and IIS?SELECT FOR XML and IIS?
Utilize ADO FunctionalityUtilize ADO Functionality
Calls made through ADO CommandCalls made through ADO Command Uses ADO Stream ObjectUses ADO Stream Object Requires ADO 2.6Requires ADO 2.6
Stream Object defined as a property of Stream Object defined as a property of the Command Objectthe Command Object
XML Data placed in the Stream XML Data placed in the Stream ObjectObject Obtained using ReadTextObtained using ReadText
ADO supports XMLADO supports XML
Recordsets can be persisted to XMLRecordsets can be persisted to XML FileFile Stream ObjectStream Object DOM IntegrationDOM Integration IIS5 Response ObjectIIS5 Response Object
Also support for ADTGAlso support for ADTG Advanced Data Table Gram formatAdvanced Data Table Gram format
Support Hierarchical RecordsetsSupport Hierarchical Recordsets
ADO XML Stream SupportADO XML Stream Support
Can persist directly to an ADO Can persist directly to an ADO Stream ObjectStream Object
XML extracted using Stream XML extracted using Stream ReadTextReadText
XML data can then be utilized in XML data can then be utilized in memorymemory Pass XML back to clientPass XML back to client Transform to HTML using XSLTransform to HTML using XSL
ADO and DOM IntegrationADO and DOM Integration
Can persist XML directly into the Can persist XML directly into the XML DOM DocumentXML DOM Document
Recordset.Save Recordset.Save MSXML.DOMDocument, MSXML.DOMDocument, adPersistXMLadPersistXML
Can perform direct XSL transform Can perform direct XSL transform for HTML based componentsfor HTML based components
ADO and IIS5 ResponseADO and IIS5 Response
Response Object exposes and Response Object exposes and IStream InterfaceIStream Interface
Recordset.Save Response, Recordset.Save Response, adPersistXMLadPersistXML
Displays XML in browserDisplays XML in browser Avoids expensive disk operationsAvoids expensive disk operations
ADO XML ProgrammingADO XML Programming
DEMODEMO
For further InformationFor further Information
XML websiteXML websitehttp://msdn.microsoft.com/xmlhttp://msdn.microsoft.com/xml
SQL Server 2000 and IISSQL Server 2000 and IIShttp://msdn.microsoft.com/msdnmag/isshttp://msdn.microsoft.com/msdnmag/issues/0300/sql/sql.aspues/0300/sql/sql.asp
SQL Server 2000 SQL Server 2000 http://www.microsoft.com/http://www.microsoft.com/sqlsql
ADO ADO http://www.microsoft.com/adohttp://www.microsoft.com/ado
Feedback / Questions ?Feedback / Questions ?