Presentation is loading. Please wait.

Presentation is loading. Please wait.

2/23/05 CS-561 - Advanced Databases 1 XML Support in SQL Server 2000 Sriram Krishnan Kevin Menard.

Similar presentations


Presentation on theme: "2/23/05 CS-561 - Advanced Databases 1 XML Support in SQL Server 2000 Sriram Krishnan Kevin Menard."— Presentation transcript:

1 2/23/05 CS-561 - Advanced Databases 1 XML Support in SQL Server 2000 Sriram Krishnan Kevin Menard

2 2/23/05 2CS-561 - 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

3 2/23/05 3CS-561 - 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

4 2/23/05 4CS-561 - 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]

5 2/23/05 5CS-561 - 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-B105-00805F49916B------------------------------------------------------------------ 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

6 2/23/05 6CS-561 - 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-B105-00805F49916B------------------------------------------------------------------ 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.

7 2/23/05 7CS-561 - 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-B105-00805F49916B ------------------------------------------------------------------ ------------------------------------------------------------------

8 2/23/05 8CS-561 - 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

9 2/23/05 9CS-561 - Advanced Databases Universal Table Format Tag Parent Customers!1!CustomerId Customers!1 Orders!2!OrderId Orders!2!OrderDate!element ----------------------------------------------------------------------------------------- 1 NULL ALFKI Alfreds Futterkiste NULL NULL 2 1 ALFKI NULL 10643 1997-08-25T00:00:00 2 1 ALFKI NULL 10692 1997-10-03T00:00:00 1 NULL ANATR Ana Trujillo Empare NULL NULL 2 1 ANATR NULL 10308 1996-09-18T00:00:00

10 2/23/05 10CS-561 - 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-B105-00805F49916B------------------------------------------------------------------ Alfreds Futterkiste Alfreds Futterkiste 1997-08-25T00:00:00 1997-10-03T00:00:00 Ana Trujillo Emparedados y helados Ana Trujillo Emparedados y helados 1996-09-18T00:00:00

11 2/23/05 11CS-561 - 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)

12 2/23/05 12CS-561 - Advanced Databases OpenXML Example DECLARE @hDoc int EXEC sp_xml_preparedocument @hDoc output, ' 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 ' SELECT * FROM OPENXML(@hdoc, '/songs/artist/song', 2) WITH (artist varchar(30) '../@name', song varchar(50) 'name') song varchar(50) 'name') EXEC sp_xml_removedocument @hDoc 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

13 2/23/05 13CS-561 - 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

14 2/23/05 14CS-561 - 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

15 2/23/05 15CS-561 - Advanced Databases Annotated Mapping Schema (cont.) Example Schema: Example Schema:

16 2/23/05 16CS-561 - 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 http://localhost/Northwind/Schema/ Customer.XDR/Customer[@Id=A%25] http://localhost/Northwind/Schema/ Customer.XDR/Customer[@Id=A%25] 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

17 2/23/05 17CS-561 - 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

18 2/23/05 18CS-561 - 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

19 2/23/05 19CS-561 - Advanced Databases Updategrams (cont.) Updategrams can also be parameterized: Updategrams can also be parameterized:

20 2/23/05 20CS-561 - 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:

21 2/23/05 21CS-561 - 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

22 2/23/05 22CS-561 - 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

23 2/23/05 23CS-561 - 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

24 2/23/05 24CS-561 - 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

25 2/23/05 25CS-561 - 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 http://localhost/Northwind?sql=SELECT +*+FROM+Customers+WHERE+CustomerId='A LFKI'+OR+CustomerId='ANATR'+FOR+XML+A UTO &root=CustomerList http://localhost/Northwind?sql=SELECT +*+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

26 2/23/05 26CS-561 - 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 http://localhost/Northwind?sql=SELECT+CustomerI d,+CompanyName+FROM+Customers+FOR+XML+AUTO&root =CustomerList&xsl=CustomerList.xsl http://localhost/Northwind?sql=SELECT+CustomerI d,+CompanyName+FROM+Customers+FOR+XML+AUTO&root =CustomerList&xsl=CustomerList.xsl http://localhost/Northwind?sql=SELECT+CustomerId,+Com panyName+FROM+Customers+FOR+XML+AUTO&root=CustomerLis t&xsl=CustomerList.xsl&contenttype=text/xml http://localhost/Northwind?sql=SELECT+CustomerId,+Com panyName+FROM+Customers+FOR+XML+AUTO&root=CustomerLis t&xsl=CustomerList.xsl&contenttype=text/xml

27 2/23/05 27CS-561 - Advanced Databases Executing Stored Procedures via URL Stored Procedure: Stored Procedure: CREATE PROC ListCustomersXML @CustomerId varchar(10)='%', @CompanyName varchar(80)='%' AS SELECT CustomerId, CompanyName FROM Customers WHERE CustomerId LIKE @CustomerId AND CompanyName LIKE @CompanyName FOR XML AUTO URL for executing stored procedure: URL for executing stored procedure: http://localhost/Northwind?sql=EXEC+ListCustomersXML +@CustomerId='A%25',@CompanyName='An%25'&root=Custo merList

28 2/23/05 28CS-561 - 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

29 2/23/05 29CS-561 - Advanced Databases Sample Template SELECT CustomerId, CompanyName FROM Customers FOR XML AUTO Example invocation: Example invocation: http://localhost/Northwind/templates/CustomerList.XML Specify a style sheet to apply to a template query: Specify a style sheet to apply to a template query: http://localhost/Northwind/Templates/CustomerList3.XML?xsl =Templates/CustomerList3.xsl&contenttype=text/html

30 2/23/05 30CS-561 - 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 LIKE @CustomerId FOR XML AUTO SELECT CustomerId, CompanyName FROM Customers WHERE CustomerId LIKE @CustomerId FOR XML AUTO Example invocation: Example invocation: http://localhost/Northwind/Templates/CustomerL ist2.XML? CustomerId=A%25

31 2/23/05 31CS-561 - 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, 2001 http://msdn.microsoft.com/xml/default.aspx?pull=/library/en- us/dnexxml/html/xml07162001.asp http://msdn.microsoft.com/xml/default.aspx?pull=/library/en- us/dnexxml/html/xml07162001.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


Download ppt "2/23/05 CS-561 - Advanced Databases 1 XML Support in SQL Server 2000 Sriram Krishnan Kevin Menard."

Similar presentations


Ads by Google