2/23/05 CS Advanced Databases 1 XML Support in SQL Server 2000 Sriram Krishnan Kevin Menard
2/23/05 2CS Advanced Databases SQL Server - XML SQL Server 2000 is an XML-enabled DBMS: SQL Server 2000 is an XML-enabled DBMS: It can read and write XML data It can read and write XML data It can return data from databases in XML format It can return data from databases in XML format It can read and update data stored in XML documents It can read and update data stored in XML documents
2/23/05 3CS Advanced Databases SQL Server's XML Features FOR XML FOR XML An extension to SELECT - allows result sets as XML An extension to SELECT - allows result sets as XML OpenXML OpenXML Allows reading and writing of data in XML documents Allows reading and writing of data in XML documents XPath queries XPath queries Allows SQL Server databases to be queried using XPath syntax Allows SQL Server databases to be queried using XPath syntax Schemas Supports XDR mapping schema and XPath queries against them Updategrams XML templates for data modifications XML Bulk Load A high-speed facility for loading XML data into a SQL Server
2/23/05 4CS Advanced Databases SELECT … FOR XML FOR XML causes SELECT to return query results as an XML stream FOR XML causes SELECT to return query results as an XML stream Three formats: RAW, AUTO, or EXPLICIT Three formats: RAW, AUTO, or EXPLICIT SELECT column list FROM table list WHERE filter criteria FOR XML RAW | AUTO | EXPLICIT [, XMLDATA] [, ELEMENTS] [, BINARY BASE64] SELECT column list FROM table list WHERE filter criteria FOR XML RAW | AUTO | EXPLICIT [, XMLDATA] [, ELEMENTS] [, BINARY BASE64]
2/23/05 5CS Advanced Databases FOR XML -- Raw Mode SELECT CustomerId, CompanyName FROM Customers FOR XML RAW: SELECT CustomerId, CompanyName FROM Customers FOR XML RAW:XML_F52E2B61-18A1-11d1-B F49916B Columns Attributes Columns Attributes Rows Generic row element Rows Generic row element XML returned is not well-formed XML returned is not well-formed Lacks a root element – must be generated by the client Lacks a root element – must be generated by the client
2/23/05 6CS Advanced Databases FOR XML -- Auto Mode SELECT CustomerId, CompanyName FROM Customers FOR XML AUTO SELECT CustomerId, CompanyName FROM Customers FOR XML AUTOXML_F52E2B61-18A1-11d1-B F49916B Each row in the result set is named after the table or view Each row in the result set is named after the table or view For results with more than one row, this amounts to having more than one top-level (root) element in the fragment, which isn't allowed in XML For results with more than one row, this amounts to having more than one top-level (root) element in the fragment, which isn't allowed in XML The rows from joined tables are nested within one another. The rows from joined tables are nested within one another.
2/23/05 7CS Advanced Databases FOR XML -- Auto Mode (cont.) Example of a JOIN query: Example of a JOIN query: SELECT Customers.CustomerID, CompanyName, OrderId FROM Customers JOIN Orders ON (Customers.CustomerId=Orders.CustomerId) FOR XML AUTO XML_F52E2B61-18A1-11d1-B F49916B </Customers> </Customers> </Customers>
2/23/05 8CS Advanced Databases FOR XML – Explicit Mode More flexible and more complicated than either raw mode or auto mode More flexible and more complicated than either raw mode or auto mode Explicit mode queries define XML documents in terms of a universal table format Explicit mode queries define XML documents in terms of a universal table format A mechanism for describing the format of XML document returned A mechanism for describing the format of XML document returned A universal table is just a SQL Server result set with special column headings that tell the server how to produce an XML document from your data A universal table is just a SQL Server result set with special column headings that tell the server how to produce an XML document from your data Element!Tag!Attribute!Directive Element!Tag!Attribute!Directive
2/23/05 9CS Advanced Databases Universal Table Format Tag Parent Customers!1!CustomerId Customers!1 Orders!2!OrderId Orders!2!OrderDate!element NULL ALFKI Alfreds Futterkiste NULL NULL 2 1 ALFKI NULL T00:00: ALFKI NULL T00:00:00 1 NULL ANATR Ana Trujillo Empare NULL NULL 2 1 ANATR NULL T00:00:00
2/23/05 10CS Advanced Databases Explicit Mode – Complex Query Links the Customers and Orders tables using the CustomerId column Links the Customers and Orders tables using the CustomerId column The Tag and Parent values in the second query link it to the first The Tag and Parent values in the second query link it to the first SELECT 1 AS Tag, NULL AS Parent, CustomerId AS [Customers!1!CustomerId], CompanyName AS [Customers!1], NULL AS [Orders!2!OrderId], NULL AS [Orders!2!OrderDate!element] FROM Customers UNION SELECT 2 AS Tag, 1 AS Parent, CustomerId, NULL, OrderId, OrderDate FROM Orders ORDER BY [Customers!1!CustomerId], [Orders!2!OrderDate!element] FOR XML EXPLICIT XML_F52E2B61-18A1-11d1-B F49916B Alfreds Futterkiste Alfreds Futterkiste <OrderDate> T00:00:00</OrderDate></Orders> <OrderDate> T00:00:00</OrderDate></Orders></Customers> Ana Trujillo Emparedados y helados Ana Trujillo Emparedados y helados <OrderDate> T00:00:00</OrderDate></Orders></Customers>
2/23/05 11CS Advanced Databases OpenXML OpenXML is a built-in Transact-SQL function that can return an XML document as a rowset OpenXML is a built-in Transact-SQL function that can return an XML document as a rowset Syntax: Syntax: OpenXML(hdoc, RowPattern [, Flag] [WITH SchemaDeclaration | TableName] OpenXML(hdoc, RowPattern [, Flag] [WITH SchemaDeclaration | TableName] hdoc = Handle to XML Document hdoc = Handle to XML Document Returned from sp_xml_preparedocument Returned from sp_xml_preparedocument RowPattern = XPath expression that identifies rows RowPattern = XPath expression that identifies rows Flag = Attribute or element-centric column patterns Flag = Attribute or element-centric column patterns WITH = Shredded rowset based upon additional parameters (omission of WITH = edge table view) WITH = Shredded rowset based upon additional parameters (omission of WITH = edge table view)
2/23/05 12CS Advanced Databases OpenXML Example int EXEC output, '<songs> It Was Almost Like a Song It Was Almost Like a Song I See Your Face Before Me I See Your Face Before Me Easy Living Easy Living Sonny Cried Sonny Cried A Nightingale Sang A Nightingale Sang You Didn't Know Me When You Didn't Know Me When </songs>' SELECT * FROM '/songs/artist/song', 2) WITH (artist varchar(30) song varchar(50) 'name') song varchar(50) 'name') EXEC artist song Johnny Hartman It Was Almost Like a Song Johnny Hartman I See Your Face Before Me Johnny Hartman Easy Living Harry Connick Jr. Sonny Cried Harry Connick Jr. A Nightingale Sang, Harry Connick Jr. You Didn't Know Me When
2/23/05 13CS Advanced Databases XML Mapping Schema XML schemas are XML documents that define the type of data that other XML documents may contain XML schemas are XML documents that define the type of data that other XML documents may contain Replacement for DTD Replacement for DTD A mapping schema is a special type of schema that maps data between an XML document and a relational table A mapping schema is a special type of schema that maps data between an XML document and a relational table Can be used to create an XML view of a SQL Server table Can be used to create an XML view of a SQL Server table SQL Server's XML schema support is based on XML-Data Reduced (XDR) SQL Server's XML schema support is based on XML-Data Reduced (XDR) An XML-Data subset that can be used to define schemas An XML-Data subset that can be used to define schemas
2/23/05 14CS Advanced Databases Annotated Mapping Schema An annotated schema is a mapping schema with special annotations (from the XML-SQL namespace) that link elements and attributes with tables and columns An annotated schema is a mapping schema with special annotations (from the XML-SQL namespace) that link elements and attributes with tables and columns Table element (default) Table element (default) Column attribute (default) Column attribute (default) Provides same level of granularity as FOR … XML EXPLICIT, without having to use universal tables Provides same level of granularity as FOR … XML EXPLICIT, without having to use universal tables
2/23/05 15CS Advanced Databases Annotated Mapping Schema (cont.) Example Schema: Example Schema: </ElementType></Schema>
2/23/05 16CS Advanced Databases Querying Using XPath XPath is a tree navigation language defined by W3C XPath is a tree navigation language defined by W3C SQL Server uses XPath to select data from XML views provided by annoted schema SQL Server uses XPath to select data from XML views provided by annoted schema XPath query can be passed via URL or template or via SQLOLEDB provider XPath query can be passed via URL or template or via SQLOLEDB provider
2/23/05 17CS Advanced Databases Updategrams Updategrams provide an XML-based method to update database Updategrams provide an XML-based method to update database Templates with special attributes and elements Templates with special attributes and elements Specify the data to update, how to update it Specify the data to update, how to update it All the execution mechanisms available with templates work equally well with updategrams All the execution mechanisms available with templates work equally well with updategrams POST, save to file and execute via URL, via ADO POST, save to file and execute via URL, via ADO
2/23/05 18CS Advanced Databases Updategrams (cont.) Each updategram: Each updategram: Contains the data changes in the form of before and after elements. Contains the data changes in the form of before and after elements. Before element contains the before image of the data to be changed Before element contains the before image of the data to be changed Row deletions Row deletions Have before image but no after image Have before image but no after image Row Insertions Row Insertions Have an after image but no before image Have an after image but no before image
2/23/05 19CS Advanced Databases Updategrams (cont.) <updg:sync><updg:before> </updg:before><updg:after> </updg:sync></employeeupdate> Updategrams can also be parameterized: Updategrams can also be parameterized:<updg:header> </updg:header>
2/23/05 20CS Advanced Databases Updategrams (cont.) Updategrams can also use XDR mapping schemas: Updategrams can also use XDR mapping schemas: Where the XDR schema is given by: Where the XDR schema is given by:
2/23/05 21CS Advanced Databases XML Bulk Load Updategrams and OpenXML, are not suitable for loading large amounts of data Updategrams and OpenXML, are not suitable for loading large amounts of data SQLXML provides a facility called the XML Bulk Load SQLXML provides a facility called the XML Bulk Load COM object COM object The first step in using the XML Bulk Load is to define a mapping schema that maps the XML data to tables and columns in database The first step in using the XML Bulk Load is to define a mapping schema that maps the XML data to tables and columns in database When the component loads the XML data, it will read it as a stream and use the mapping schema to decide where the data goes in the database When the component loads the XML data, it will read it as a stream and use the mapping schema to decide where the data goes in the database
2/23/05 22CS Advanced Databases XML Bulk Load (cont.) VB Example: VB Example: Set objBulkLoad=CreateObject("SQLXMLBulkLoad.S QLXMLBulkLoad") objBulkLoad.ConnectionString = "provider=SQLOLEDB;data source=SuperServer;database=Northwind;" objBulkLoad.Execute "d:\xml\OrdersSchema.xdr", "d:\xml\OrdersData.xml" Set objBulkLoad = Nothing
2/23/05 23CS Advanced Databases Accessing SQL Server Over HTTP SQL Server's ability to publish data over HTTP is made possible through SQLISAPI with IIS SQL Server's ability to publish data over HTTP is made possible through SQLISAPI with IIS An Internet Server API (ISAPI) extension An Internet Server API (ISAPI) extension SQLISAPI uses SQLOLEDB, SQL Server's native OLE DB provider, to access the database associated with a virtual directory SQLISAPI uses SQLOLEDB, SQL Server's native OLE DB provider, to access the database associated with a virtual directory Configuring a virtual directory allows SQL Server's XML features via HTTP Configuring a virtual directory allows SQL Server's XML features via HTTP
2/23/05 24CS Advanced Databases Accessing SQL Server over HTTP (cont.) Private Intranet Private Intranet Send a SELECT … FOR XML query string in URL Send a SELECT … FOR XML query string in URL Post an XML query template to SQLISAPI Post an XML query template to SQLISAPI Public Internet Public Internet Specify a server-side XML schema Specify a server-side XML schema Specify a server-side XML query template Specify a server-side XML query template
2/23/05 25CS Advanced Databases URL Queries URL queries allow users to specify a complete Transact-SQL query via a URL URL queries allow users to specify a complete Transact-SQL query via a URL +*+FROM+Customers+WHERE+CustomerId='A LFKI'+OR+CustomerId='ANATR'+FOR+XML+A UTO &root=CustomerList +*+FROM+Customers+WHERE+CustomerId='A LFKI'+OR+CustomerId='ANATR'+FOR+XML+A UTO &root=CustomerList The first parameter we pass here is sql The first parameter we pass here is sql The second parameter specifies the name of the root element for the XML document that will be returned The second parameter specifies the name of the root element for the XML document that will be returned
2/23/05 26CS Advanced Databases URL Queries (cont.) URL query can also include the xsl parameter URL query can also include the xsl parameter Translates the XML document that's returned by the query into a different format Translates the XML document that's returned by the query into a different format d,+CompanyName+FROM+Customers+FOR+XML+AUTO&root =CustomerList&xsl=CustomerList.xsl d,+CompanyName+FROM+Customers+FOR+XML+AUTO&root =CustomerList&xsl=CustomerList.xsl panyName+FROM+Customers+FOR+XML+AUTO&root=CustomerLis t&xsl=CustomerList.xsl&contenttype=text/xml panyName+FROM+Customers+FOR+XML+AUTO&root=CustomerLis t&xsl=CustomerList.xsl&contenttype=text/xml
2/23/05 27CS Advanced Databases Executing Stored Procedures via URL Stored Procedure: Stored Procedure: CREATE PROC varchar(80)='%' AS SELECT CustomerId, CompanyName FROM Customers WHERE CustomerId AND CompanyName FOR XML AUTO URL for executing stored procedure: URL for executing stored procedure: merList
2/23/05 28CS Advanced Databases Template Queries Templates are XML documents based on the XML- SQL namespace Templates are XML documents based on the XML- SQL namespace Mechanism for translating a URL into a query that SQL Server can process Mechanism for translating a URL into a query that SQL Server can process Safer and more widely used technique for retrieving data over HTTP Safer and more widely used technique for retrieving data over HTTP End users never see the source code End users never see the source code Templates are stored on the Web server Templates are stored on the Web server Referenced via a virtual name Referenced via a virtual name
2/23/05 29CS Advanced Databases Sample Template <sql:query> SELECT CustomerId, CompanyName FROM Customers FOR XML AUTO </sql:query></CustomerList> Example invocation: Example invocation: Specify a style sheet to apply to a template query: Specify a style sheet to apply to a template query: =Templates/CustomerList3.xsl&contenttype=text/html
2/23/05 30CS Advanced Databases Templates Parameterized Templates Parameterized Templates Permit the user to supply parameters to the query Permit the user to supply parameters to the query % % SELECT CustomerId, CompanyName FROM Customers WHERE CustomerId FOR XML AUTO SELECT CustomerId, CompanyName FROM Customers WHERE CustomerId FOR XML AUTO </CustomerList> Example invocation: Example invocation: ist2.XML? CustomerId=A%25
2/23/05 31CS Advanced Databases Conclusions SQL Server 2000 has a lot of ways to work with XML, suitable for a number of situations SQL Server 2000 has a lot of ways to work with XML, suitable for a number of situations Questions? Questions? References: References: Conrad, Andrew. A Survey of Microsoft SQL Server 2000 XML Features: Microsoft Corporation, 2001 Conrad, Andrew. A Survey of Microsoft SQL Server 2000 XML Features: Microsoft Corporation, us/dnexxml/html/xml asp us/dnexxml/html/xml asp Henderson, Ken. Guru's Guide to SQL Server Architecture and Internals, The (Chapter: Using SQL Server's XML Support) Henderson, Ken. Guru's Guide to SQL Server Architecture and Internals, The (Chapter: Using SQL Server's XML Support) Rys, Michael. Bringing the Internet to Your Database: Using SQL Server 2000 and XML to Build Loosely-Coupled Systems: Microsoft Corporation Rys, Michael. Bringing the Internet to Your Database: Using SQL Server 2000 and XML to Build Loosely-Coupled Systems: Microsoft Corporation