Chapter 14 1 Chapter 14 Storing and Retrieving XML in SQL Server 2000 November 6, 2001 Sook-Kyo Kwon.

Slides:



Advertisements
Similar presentations
2/23/05 CS Advanced Databases 1 XML Support in SQL Server 2000 Sriram Krishnan Kevin Menard.
Advertisements

XML: Extensible Markup Language
Michael Pizzo Software Architect Data Programmability Microsoft Corporation.
SQL Language. Introduction to RDBMS Introduction to RDBMS Basic Data Manipulation - Reading Data Basic Data Manipulation - Reading Data Basic Data Manipulation.
Inside Data Access with SQLXML: Architecture Guide Rolandas Gricius MCT, MCSE, MCSD Adapted From.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
Introduction to Structured Query Language (SQL)
Module 9: Implementing Stored Procedures. Introduction to Stored Procedures Creating Executing Modifying Dropping Using Parameters in Stored Procedures.
Tools of the trade TSQL CIS 407. SQL Server Tools Books on line! Don’t use sql server authentication –Use windows authentication (safer) for developer.
Copyright 2000, Microsoft Corp. SQL Server 2000 XML Annotated Schemata Michael Rys Program Manager SQLServer XML Technologies Microsoft Corporation
1 Storing XML using Relational Model XML and data management By: Habiba Skalli For: Dr. Haddouti.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Module 9 Designing an XML Strategy. Module 9: Designing an XML Strategy Designing XML Storage Designing a Data Conversion Strategy Designing an XML Query.
SQL Server 2000 and XML Erik Veerman Consultant Intellinet Business Intelligence.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
Using SQL Queries to Generate XML- Formatted Data Joline Morrison Mike Morrison Department of Computer Science University of Wisconsin-Eau Claire.
DAT304 Leveraging XML and HTTP with Sql Server Irwin Dolobowsky Program Manager Webdata Group.
2.2 SQL Server 2005 的 XML 支援功能. Overview XML Enhancements in SQL Server 2005 The xml Data Type Using XQuery.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Integrating XML with Microsoft SQL Server ©NIITeXtensible Markup Language/Lesson 9/Slide 1 of 31 Objectives In this lesson, you will learn to: * Generate.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
ASP.NET Programming with C# and SQL Server First Edition
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
SQL Review Tonga Institute of Higher Education. SQL Introduction SQL (Structured Query Language) a language that allows a developer to work with data.
Defining Stored Procedures Named Collections of Transact-SQL Statements Encapsulate Repetitive Tasks Five Types (System, Local, Temporary, Remote, and.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 27 The World Wide Web and XML. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.27-2 Topics in this Chapter The Web and the Internet.
Discovering Computers Fundamentals Fifth Edition Chapter 9 Database Management.
5/24/01 Leveraging SQL Server 2000 in ColdFusion Applications December 9, 2003 Chris Lomvardias SRA International
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
1 Design Issues in XML Databases Ref: Designing XML Databases by Mark Graves.
Module 18 Querying XML Data in SQL Server® 2008 R2.
Databases Shortfalls of file management systems Structure of a database Database administration Database Management system Hierarchical Databases Network.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Retrieving XML Data from SQL server.  Using the FOR XML Clause to Retrieve Data Retrieving Data in XML Format How SQL Server Generates XML Using the.
Chapter 27 The World Wide Web and XML. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.27-2 Topics in this Chapter The Web and the Internet.
Session 11 Creating Tables and Using Data Types. RDBMS and Data Management/Session 11/2 of 40 Session Objectives Define the data types and list the categories.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
XML and Database.
CHAPTER 9 SQL อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Understanding Databases Lesson 6. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understanding Relational Database Concepts Understand relational.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Session 1 Module 1: Introduction to Data Integrity
SQLXML XML Technology For SQL Server Brian Moore Developer and Platform Strategy Group Microsoft Corporation.
Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.
DAT 405 Extreme SqlXml Irwin Dolobowsky Program Manager – SqlXml Microsoft Corporation.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Dev399 SQLXML: XML Views and Relational Data Irwin Dolobowsky Program Manager Webdata Group Microsoft Corporation.
Copyright 2002, Ronald Bourret, XML-DBMS Middleware for XML and databases Ronald Bourret O'Reilly Open.
 CONACT UC:  Magnific training   
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
11 Copyright © 2004, Oracle. All rights reserved. Managing XML Data in an Oracle 10g Database.
XML and SQL Server Better friends than you thought Matt Hartman.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
DAT 400 Inside Data Access with SQLXML: Architecture Guide Alexander Vaschillo Lead Program Manager WebData XML Group Microsoft Corporation.
CompSci 280 S Introduction to Software Development
Fundamentals of DBMS Notes-1.
XML: Extensible Markup Language
All about JSON Scenarios and value proposition for JSON data enabled in Azure SQL Database and SQL Server Ralph Kemperdick Digital Business Architect,
Chapter 5 Introduction to SQL.
Using XML in SQL Server and Azure SQL Database
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
RELATIONAL DATABASES AND XML
Contents Preface I Introduction Lesson Objectives I-2
Presentation transcript:

Chapter 14 1 Chapter 14 Storing and Retrieving XML in SQL Server 2000 November 6, 2001 Sook-Kyo Kwon

Chapter 14 2 Contents Objectives Retrieving XML : FOR XML Storing XML : OPENXML XML Bulk Load Summary

Chapter 14 3 Objectives Eliminate to write complex ASP(Active Server Pages) applications SQL Server 2000 includes XML supports – Allow to view the entire relational database as XML Server side features – FOR XML : retrieve data from SQL ser as an XML –OPENXML : shred XML documents and store them in relational tables – XML Bulk Load : bulk load from an XML document into SQL – XML Views : provide an XML view of relational data – Xpath query support : query the XML view – XML Updategrams : update data in relational tables using XML views  Enhance query performance and system performance in updating

Chapter 14 4 Retrieving XML from SQL server 2000: FOR XML (1) Description SQL server 2000 provides enhanced query support We can request the result of a SELECT statement be returned as an XML To retrieve the result of SELECT statement as XML, we must specify the FOR XML clause in the SELECT statement. Three Modes RAW Mode : produces attribute-centric XML with flat structure AUTO Mode : produces XML where the hierarchy of elements in the resulting XML is determined by the order of columns in the SELECT statements.  have limited control over the shape of XML produced. EXPLICIT Mode : allows the user to have total control over the shape of the resulting XML.

Chapter 14 5 Retrieving XML from SQL server 2000: FOR XML (2) FOR XML : General Syntax XMLDATA is specified : XML-Data schema for the resulting XML is returned as a part of the result. ELEMENTS is specified : an element-centric documents is to be returned in which the column values are returned as sub-elements. Only applies in AUTO mode. BINARY Base64 is specified: any binary data returned is represented in base64-encoded format. This should be specified in RAW and EXPILICT. The AUTO mode returns data as a URL reference by default. FOR XML xml_mode [,XMLDATA], [,ELEMENTS], [BINARY BASE64]

Chapter 14 6 Retrieving XML from SQL server 2000: FOR XML (3) The RAW Mode(1) If the RAW mode is specified, each row in the rowset returned by the SELECT statement is transformed into an element with generic tag and the columns as attribute. Query (1) SELECT C.CustomerID, O.OrderID, OorderDate FROMCustomers C, Order O WHEREC.CustomerID = O.CustomerID ORDER BY C.CustoerID, O.OrderID FOR XML RAW 

Chapter 14 7 Retrieving XML from SQL server 2000: FOR XML (4) The RAW Mode (2) Request XDR schema for the resulting XML Query (2) SELECT C.CustomerID, O.OrderID, OorderDate FROMCustomers C, Order O WHEREC.CustomerID = O.CustomerID ORDER BY C.CustoerID, O.OrderID XMLDATA FOR XML RAW, XMLDATA <Schema name=“Schema1” xmlns=“urn:schemas-micrsoft-com:xml-data” xmlns:dt=“urn:schemas-microsoft-com:datatypes”> 

Chapter 14 8 Retrieving XML from SQL server 2000: FOR XML (5) The AUTO Mode (1) The query specified with AUTO mode generates hierarchical XML Query (3) SELECT Customers.CustomerID, ContactName, Order ID, OrderDate FROMCustomers, Orders WHERECustomers.CustomerID = Orders.CustomerID ORDER BY Customers.CustoerID, Orders.OrderID FOR XML AUTO <Orders …… …… …… 

Chapter 14 9 Retrieving XML from SQL server 2000: FOR XML (6) The AUTO Mode (2) The ELEMENTS option: each column name maps to sub-elements with text only contents. Query (4) SELECT Customers.CustomerID, ContactName, Order ID, OrderDate FROMCustomers, Orders WHERECustomers.CustomerID = Orders.CustomerID ORDER BY Customers.CustoerID, Orders.OrderID ELEMENTS FOR XML AUTO, ELEMENTS ALFKI Maria Anders T00:00:00 ….. ANATR Ana Trujillo T00:00:00 …… 

Chapter Retrieving XML from SQL server 2000: FOR XML (7) The EXPLICIT Mode (1) Allows you total control over the resulting XML. We can decide the shape of the resulting XML. 1.Specifying Column Aliases In SELECT clauses, the column aliases should be specified. Information in the column aliases is used to generate the XML hierarchy. SELECT 1as Tag, NULLas Parent, Customers.CustomerIDas [Cust!1!CustID] Customers.ContactNameas [Cust!1!Contact] FROMCustomers ORDER BY [Cust!1CustID] FOR XML EXPLICIT <Cust CustID=“ANATR” Contact=“Ana Trujillo” 

Chapter Retrieving XML from SQL server 2000: FOR XML (8) The EXPLICIT Mode (2) 2. Specifying The Metadata Column –Tag and Parent columns are providing parent-child relationship information between elements in XML that is generated by the query. –Tag column provides a numeric tag number of the element –Parent column is used to define which element is the parent of this element SELECT 1as Tag, NULLas Parent, Customers.CustomerIDas [Cust!1!CustID] Customers.ContactNameas [Cust!1!Contact] FROMCustomers ORDER BY [Cust!1CustID] FOR XML EXPLICIT

Chapter Retrieving XML from SQL server 2000: FOR XML (9) The EXPLICIT Mode (3) 3. Specifying The Directive in the Column Alias (1) –Identify certain attributes in the query as being of type id,idref, or idrefs. –Request the resulting XML be an element-centric document –Wrap data in the XML in CDATA section –Specify how to deal with characters in the data returned by SQL server that are special in XML SELECT 1as Tag, NULLas Parent, Customers.CustomerIDas [Cust!1!CustID!id] Customers.ContactNameas [Cust!1!Contact] FROMCustomers ORDER BY [Cust!1CustID!id] XMLDATA FOR XML EXPLICIT,XMLDATA <Schema name=“Schema2” xmlns=“urn:schemas-micrsoft-com:xml-data” xmlns:dt=“urn:schemas-microsoft-com:datatypes”> … 

Chapter Retrieving XML from SQL server 2000: FOR XML (10) The EXPLICIT Mode (4) 3. Specifying The Directive in the Column Alias (2) SELECT 1as Tag, NULLas Parent, Customers.CustomerIDas [Cust!1!CustID!element] Customers.ContactNameas [Cust!1!Contact!element] FROMCustomers ORDER BY [Cust!1CustID!element] FOR XML EXPLICIT  ALFKI Maria Anders ANATR Ana Trujillo … SELECT 1as Tag, NULLas Parent, Customers.CustomerIDas [Cust!1!CustID] Customers.ContactNameas [Cust!1!Contact!element] FROMCustomers ORDER BY [Cust!1CustID] FOR XML EXPLICIT Maria Anders Ana Trujillo … 

Chapter Retrieving XML from SQL server 2000: FOR XML (11) The EXPLICIT Mode (5) Generating XML from the Rowset(Universal Table) SELECT 1as Tag, NULLas Parent, Customers.CustomerIDas [Cust!1!CustID] Customers.ContactNameas [Cust!1!Contact] FROMCustomers ORDER BY [Cust!1CustID] FOR XML EXPLICIT TagParentCust!1!CustIDCust!1!Contact NULL“ALFKI”“Maria Andrers” 1NULL“ANATR”“Ana Trujillo” 1NULL“ANTON”“Antonio Moreno” > …  

Chapter Retrieving XML from SQL server 2000: FOR XML (12) The EXPLICIT Mode (6) Hierarchy Generation SELECT 1as Tag, NULLas Parent, Customers.CustomerIDas [Custtomer!1!CustomerID] NULLas [Order!2!OrderID] FROMCustomers TagParent Customer!1!CustomerIDOrder!2!OrderID NULLALFKINULL 1NULLANATRNULL   TagParent Customer!1!CustomerIDOrder!2!OrderID ALFKI ALFKI ANATR ANATR10625 SELECT 2, 1, Customers.CustomerID Orders.OrderID FROMCustomers,Orders WHERECustomers.CustomerID = Orders.CustomerID

Chapter Retrieving XML from SQL server 2000: FOR XML (13) The EXPLICIT Mode (7) Hierarchy Generation – Union All SELECT 1as Tag, NULLas Parent, Customers.CustomerIDas [Custtomer!1!CustomerID] NULLas [Order!2!OrderID] FROMCustomers UNION ALL SELECT 2, 1, Customers.CustomerID Orders.OrderID FROMCustomers,Orders WHERECustomers.CustomerID = Orders.CustomerID ORDER BY[Customer!1!CustomerID], [Order!2!OrderID] FOR XML EXLICIT  Rowset (Universal Table) TagParent Customer!1!CustomerIDOrder!2!OrderID NULL ALFKINULL 21 ALFKI ALFKI ALFKI… 1NULL ANATRNULL 21 ANATR ANATR ANATR…..

Chapter Retrieving XML from SQL server 2000: FOR XML (14) The EXPLICIT Mode (8) Hierarchy Generation – Union All:Resulting XML …… …… ……

Chapter Storing XML in SQL server 2000: OPENXML (1) Description OPENXML function in SQL Server 2000 is a rowset provider. Create a rowset from the XML daat and pass it to the INSERT, UPDATE, or DELETE statement. The OPENXML feature in SQL server 2000 allows you to store data from XML documents or document fragments in database tables.STEPS Create an in-memory DOM representation of the XML document Use OPENXML to create a rowset view of this XML. As part of OPENXML, specify an Xpath expression to retrieve the desired elements. Pass this rowset to INSERT, UPDATE, and DELETE statemens to update the database. Destroy the in-memory DOM representation of the XML document

Chapter Storing XML in SQL server 2000: OPENXML (2) OPENXML : General Syntax DocHandle: XML handle returned by sp_xml_preparedocument XpathPattern: identifies the nodes in the XML document that will be mapped to the rowset generated. (ex.: Xpath pattern /root/Order/OrderDetail identifies the child element nodes of the child element node of the element. Flags:specifies how the attributes/sub-elements in the XML document map to the columns of the rowset being generated. (1 for attribute-centric, 2 for element-centric, 3 for mixed) WITH Clause: used to provide the description of the rowset schema to generate. –Don’t specify, Specify an existing table name, Specify the rowset schema yourself OPENXML (DocHandleint, XPathPatternnvarchar, [Flagsbyte]) [WITH (RowsetSchema | Tab;eName]

Chapter Storing XML in SQL server 2000: OPENXML (3) EXAMPLE –CustOrder Table CustomerOrder(oid varchar(10), orderdate datetime, requireddate datetime) –XML Document <Order oid=“Ord1” empid=“1” orderdate=“10/1/2000” requireddate=“11/1/2000” note=“ship 2 nd day UPS” /> <Order oid=“Ord2” empid=“1” orderdate=“10/2/2000” requireddate=“12/1/2000” /> <Order oid=“Ord3” empid=“2” orderdate=“9/1/2000” requireddate=“10/1/2000” <Order oid=“Ord4” empid=“3” orderdate=“9/2/2000” requireddate=“10/2/2000” />  SELECT * FROM OPENXML ‘/root/Customer/Order’) WITH CustOder The document handle and the table name are passed to OPENXML

Chapter Storing XML in SQL server 2000: OPENXML (4) oidorderdaterequireddate Ord :00: :00: Ord :00: :00: Ord :00: :00: Ord :00: :00: INSERT INTO CustOrder SELECT * FROM OPENXML ‘/root/Customer/Order’) WITH CustOder The resulting three-column rowset returned by the SELECT statement UPDATE CustOrder SET requireddate = (SELECT requireddate FROM OPENXML ‘/root/Customer/Order’) WITH CustOrder WHERE oid = ‘Oid1’) SELECT * FROM OPENXML ‘/root/Customer/Order’) WITH (oidvarchar(20) orderdatedatetime orderdatedatetime requireddatedatetime) requireddatedatetime) Specify rowset schema

Chapter Storing XML in SQL server 2000: OPENXML (5) Attribut-Centric and Element-centric Mapping int varchar(1000) 10/1/2000/ 11/1/2000 note=“ship 2 nd day UPS” 10/2/2000/ 12/1/2000 9/1/2000/ 10/1/2000 9/2/2000/ 10/21/2000 EXEC EXEC SELECT * FROM OPENXML ‘/root/Customer/Order’,3) WITH (oidvarchar(20) orderdatedatetime orderdatedatetime requireddatedatetime) requireddatedatetime)

Chapter Storing XML in SQL server 2000: OPENXML (6) OrdIDOrdDateOrdReqDate Ord :00: :00: Ord :00: :00: Ord :00: :00: Ord :00: :00: Additional Mapping Information : genral syntax ColumnName datatpe [AdditonalMapping] SELECT * FROM OPENXML ‘/root/Customer/Order’,1) WITH (OrdIDvarchar(20) OrdDatedatetime OrdDatedatetime OrdReqDatedatetime OrdReqDatedatetime

Chapter Storing XML in SQL server 2000: OPENXML (7) oidCustNameUniqueIDValNodeNameNodeSibling Ord1Bob6OrderNULL Ord2Bob12Order6 Ord3John21OrderNULL Ord4John26Order21 Metaproperty SELECT * FROM OPENXML ‘/root/Customer/Order’,3) WITH ( oid varchar(20) CustName varchar(10) CustName varchar(10) UniqueIDVal int UniqueIDVal int NodeNmae varchar(10) NodeSibiling varchar(10) ‘mp:prev’)

Chapter Bulk Loading XML (1) Description Loading large amounts of XML onto the database Use the XML Bulk Load object from code via Bulk Load object model. We need to pass it two pieces of information: –The Mapping schema: The mapping XDR schema must be provided. –The XML document that you want to bulk loadProperties Specify whether we want to execute Bulk Load in transaction mode or not. Specify if we want to tables created before loading the data. Checking any contraint violations (primary key or foreign key) Specify error logging file/message generated during execution Execute, ConnectionCommand, ConnectionString, KeepNulls, KeepIdentity, CheckConstraints, ForceTableLock, XMLFragment, Transaction, TempFilePath, ErrorLogFile, SchemaGen, SGDropTables, SGUseID

Chapter Bulk Loading XML (2) EXAMPLE CREATE TABLE Customer ( Customer ID intprimary key, CompanyName varchar(20)NOT NULL, City varchar(20)default ‘Seattle’) Go CREATE TABLE CustOrder ( OrderIDvarchar(10)primary key CustomerIDint foreign key refrences Customer (CustomerID), OrderDate datetime default ‘ ’) Go XML Data XML Data is bulk loaded in these tables

Chapter Bulk Loading XML (3) EXAMPLE – Executing Bulk Load : Visual Basic code Set objBL = CreateObject(“SQLXMLBulkLoad.SQLXMLBulkLoad”) ObjBL.ConnectionString = “proiver=SQLOLEDB.1;data “ & _ “source=server;database=database;uid=sa;pwd=“ objBL.ErrorLogFile = “c:\error.log” objBL.CheckConstraints=True objBL.XMLFragment=True objBL.Execute “c:\XMLView.xml”, “c:\XMLData.xml” Set objBL1=Nothing 1.Create a SQLXMLBulkLOad object(objBL) 2.Set various properties 3.Execute method is called. Execute method takes two parameters: the schema file name and XML data file name.