Module 3: Manipulating XML with Transact-SQL

38 358 0
Module 3: Manipulating XML with Transact-SQL

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Contents Overview 1 Creating a Rowset from an XML Document 2 Specifying the Structure of a Rowset 13 Lab 3: Using OPENXML 26 Best Practices 33 Review 34 Module 3: Manipulating XML with Transact-SQL Information in this document is subject to change without notice. The names of companies, products, people, characters, and/or data mentioned herein are fictitious and are in no way intended to represent any real individual, company, product, or event, unless otherwise noted. Complying with all applicable copyright laws is the responsibility of the user. No part of this document may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without the express written permission of Microsoft Corporation. If, however, your only means of access is electronic, permission to print one copy is hereby granted. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.  2001 Microsoft Corporation. All rights reserved. Microsoft, ActiveX, BackOffice, BizTalk, MSDN, MS-DOS, SQL Server, Visual Basic, Visual C++, Visual InterDev, Visual J++, Visual Studio, Windows, Windows Media, Windows NT, and Windows 2000 are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners. Module 3: Manipulating XML with Transact-SQL iii Instructor Notes This module provides students with an understanding of how to insert XML data into SQL Server tables by using the OPENXML statement. After completing this module, students will be able to:  Use the OPENXML statement to create a rowset from a single-level Extensible Markup Language (XML) document.  Use the OPENXML statement to process rowsets from complex XML documents.  Retrieve attributes, elements, or both from an XML document by specifying the appropriate flags parameter with the OPENXML statement.  Use XML Path Language (XPath) expressions in rowpattern and colpattern parameters to specify rowset structure. Materials and Preparation This section provides the materials and preparation tasks that you need to teach this module. Required Materials To teach this module, you need Microsoft ® PowerPoint ® file 2091A_03.ppt. Preparation Tasks To prepare for this module, you should:  Read all of the materials for this module.  Complete the lab and practice.  Practice the demonstration.  Review the multimedia animation. Presentation: 90 Minutes Lab: 30 Minutes iv Module 3: Manipulating XML with Transact-SQL Module Strategy Use the following strategies to present this module:  Creating a Rowset from an XML Document Emphasize the importance of using the sp_xml_removedocument system stored procedure to remove from memory the tree generated by the sp_xml_preparedocument system stored procedure. Emphasize that the main purpose of using OPENXML is to shred a document into one or more tables. Do not go into details about the rowpattern, flags, and Table Name parameters, and do not discuss colpattern parameters. These will be introduced in the next section.  Specifying the Structure of a Rowset Emphasize that combining flags parameters by using a logical OR can introduce performance issues. A better approach is to specify either an attribute-centric or element-centric mapping and use column patterns for non–default-mapped data. Module 3: Manipulating XML with Transact-SQL 1 Overview  Creating a Rowset from an XML Document  Specifying the Structure of a Rowset ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** After completing this module, you will be able to:  Use the OPENXML statement to create a rowset from a single-level Extensible Markup Language (XML) document.  Use the OPENXML statement to process rowsets from complex XML documents.  Retrieve attributes, elements, or both from an XML document by specifying the appropriate flags parameter with the OPENXML statement.  Use XML Path Language (XPath) expressions in rowpattern and colpattern parameters to specify rowset structure. Topic Objective To provide an overview of the module topics and objectives. Lead-in In this module, you will learn about the OPENXML statement and how you can use it to manipulate XML data in Transact-SQL. 2 Module 3: Manipulating XML with Transact-SQL    Creating a Rowset from an XML Document  Process Overview  Multimedia: Parsing and Shredding XML  Creating an Internal Tree  Retrieving a Rowset from an XML Tree  Inserting Data from XML Documents into Tables  Practice: Retrieving Rowsets with OPENXML ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** A rowset is an OLE DB object that contains the result set for a query. In a trading partner integration scenario, you might need to generate a rowset from an XML document. For example, a retailer sends orders to a supplier as XML documents. The supplier must then generate rowsets from the XML in order to insert the data into one or more tables in the database. This section discusses the use of the OPENXML statement to generate rowsets from data in XML documents. Topic Objective To introduce the topics in this section. Lead-in In an application integration scenario, you often must create a rowset from XML data in order to process it. Module 3: Manipulating XML with Transact-SQL 3 Process Overview 1. Receive an XML document • Usually a parameter to a stored procedure 2. Generate an internal tree representation • Use sp_xml_preparedocument to parse the XML 3. Retrieve a rowset from the tree • Use OPENXML 4. Process the data from the rowset • Usually “shredding” into permanent tables 5. Destroy the internal tree when it is no longer required • Use sp_xml_removedocument ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** Processing XML data as a rowset involves the following five steps: 1. Receive an XML document. When an application receives an XML document, it can process the document by using Transact-SQL code. For example, when a supplier receives an XML order from a retailer, the supplier logs the order in a Microsoft ® SQL Server ™ database. Usually, the Transact-SQL code to process the XML data is implemented in the form of a stored procedure, and the XML string is passed as a parameter. 2. Generate an internal tree representation. Before processing the document, use the sp_xml_preparedocument system stored procedure to parse the XML document and transform it into an in- memory tree structure. The tree is conceptually similar to a Document Object Model (DOM) representation of an XML document. You can use only a valid, well-formed XML document to generate the internal tree. 3. Retrieve a rowset from the tree. You use the OPENXML statement to generate an in-memory rowset from the data in the tree. Use XPath query syntax to specify the nodes in the tree to be returned in the rowset. Topic Objective To describe the process used to generate a rowset from XML. Lead-in There are five steps involved in processing XML data as a rowset. 4 Module 3: Manipulating XML with Transact-SQL 4. Process the data from the rowset. Use the rowset created by OPENXML to process the data, in the same way that you would use any other rowset. You can select, update, or delete the data by using Transact-SQL statements. The most common use of OPENXML is to insert rowset data into permanent tables in a database. For example, an XML order received by a supplier might contain data that must be inserted into OrderHeader and OrderDetails tables. 5. Destroy the internal tree when it is no longer required. Because the tree structure is held in memory, use the sp_xml_removedocument system stored procedure to free the memory when the tree is no longer required. Module 3: Manipulating XML with Transact-SQL 5 Multimedia: Parsing and Shredding XML ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** In this animation, you will observe the process used to receive an XML document and insert its data into tables in a SQL Server database. Topic Objective To introduce the animation, which is about 2 ½ minutes long. Lead-in In this animation, you will observe the process of shredding an XML document into a SQL Server database. 6 Module 3: Manipulating XML with Transact-SQL Creating an Internal Tree  Create the tree with sp_xml_preparedocument  Free memory with sp_xml_removedocument CREATE PROC ProcessOrder @doc NText AS DECLARE @idoc integer EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Process Document EXEC sp_xml_removedocument @idoc CREATE PROC ProcessOrder @doc NText AS DECLARE @idoc integer EXEC sp_xml_preparedocument @idoc OUTPUT, @doc -- Process Document EXEC sp_xml_removedocument @idoc ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** Before you can process an XML document by using Transact-SQL statements, you must parse the document and transform it into a tree structure. Parsing XML with sp_xml_preparedocument The sp_xml_preparedocument system stored procedure parses an XML document and generates an internal tree representation of the document, and has the following syntax. sp_xml_preparedocument hdoc OUTPUT [, xmltext] [, xpath_namespaces] The following table describes the parameters for the sp_xml_preparedocument system stored procedure. Parameter Description xmltext The original XML document to be processed hdoc A handle to the parsed XML tree xpath_namespaces (optional) XML namespace declarations that are used in row and column XPath expressions in OPENXML statements Topic Objective To describe the use of the sp_xml_preparedocument stored procedure. Lead-in Use the sp_xml_preparedocument stored procedure to parse an XML document and generate the internal tree. Syntax [...]... You implement the sp _xml_ preparedocument and sp _xml_ removedocument system stored procedures as extended stored procedures This means that you can call them from within your own custom stored procedures, but not from within a user-defined function 8 Module 3: Manipulating XML with Transact-SQL Retrieving a Rowset from an XML Tree Topic Objective OPENXML syntax To describe the OPENXML statement The rowpattern... to map the attributes and elements in the XML document to columns in a table that have different names from the columns in the rowset 9 10 Module 3: Manipulating XML with Transact-SQL Inserting Data from XML Documents into Tables Topic Objective To describe how OPENXML can be used to insert XML data into a table Lead-in The primary use of OPENXML is to insert XML data into tables Use an INSERT statement... use XPath expressions to retrieve nodes from an XML document Know how to write a Transact-SQL script containing an OPENXML statement Module 3: Manipulating XML with Transact-SQL 27 Lab Setup To complete this lab, you need a computer running Windows 2000 Server with SQL Server 2000 installed For More Information Review the section “Writing XML Using OpenXML” in SQL Server Books Online Scenario You are... and populates it with the result set of a SELECT statement Requires CREATE TABLE permission in the destination database Module 3: Manipulating XML with Transact-SQL Using the INSERT Statement with Existing Tables You use the INSERT statement to insert data from an XML document into an existing SQL Server table For example, use the following Transact-SQL statement to insert data from an XML document into... named neworders that contains data from an XML document Example SELECT * INTO neworders FROM OPENXML(@idoc, 'order', 1) WITH (orderno integer, [date] datetime, customer integer) This statement creates the following neworders table orderno date customer 1001 01/01/2001 1235 11 12 Module 3: Manipulating XML with Transact-SQL Practice: Retrieving Rowsets with OPENXML Topic Objective To introduce the practice... and elements Example CREATE PROC insertorder @xmlOrder NText AS INSERT orders SELECT * FROM OPENXML (@idoc, 'order', 1) WITH orders INSERT lineitems SELECT * FROM OPENXML (@idoc, 'order/lineitem', 2) WITH (orderno integer ' /@orderno', productid integer '@productid', price money '@price', quantity integer, default mapping) 22 Module 3: Manipulating XML with Transact-SQL The preceding stored procedure.. .Module 3: Manipulating XML with Transact-SQL 7 The following example shows how to use the sp _xml_ preparedocument system stored procedure to parse an XML document that has been passed to a new custom stored procedure Example CREATE PROC ProcessOrder @doc NText AS DECLARE @idoc integer EXEC sp _xml_ preparedocument @idoc OUTPUT, @doc Freeing Memory with sp _xml_ removedocument You... organization often receives XML documents from its trading partners, and data must be extracted from the documents and inserted into tables in a SQL Server database You must write the necessary OPENXML statements to extract the data Estimated time to complete this lab: 30 minutes 28 Module 3: Manipulating XML with Transact-SQL Exercise 1 Retrieving Rowsets from XML Data with OPENXML In this exercise, you... insertorder @xmlOrder NText AS INSERT orders SELECT * FROM OPENXML (@idoc, 'order', 1) WITH orders INSERT lineitems SELECT * FROM OPENXML (@idoc, 'order/lineitem', 1) WITH lineitems The preceding stored procedure inserts the following sets of data into the orders and lineitems tables orderno orderdate custid 1001 01/01/2001 1235 orderno productid price 1001 14 15.99 1001 17 5.49 Module 3: Manipulating XML with. .. 2 1 26 Module 3: Manipulating XML with Transact-SQL Lab 3: Using OPENXML Topic Objective To introduce the lab Lead-in In this lab, you will use the OPENXML statement to generate rowsets from XML documents and insert data into tables *****************************ILLEGAL FOR NON-TRAINER USE****************************** . from an XML Document 2 Specifying the Structure of a Rowset 13 Lab 3: Using OPENXML 26 Best Practices 33 Review 34 Module 3: Manipulating XML with Transact-SQL. owners. Module 3: Manipulating XML with Transact-SQL iii Instructor Notes This module provides students with an understanding of how to insert XML data

Ngày đăng: 18/10/2013, 18:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan