Download presentation
Presentation is loading. Please wait.
Published byLeo Stewart Modified over 9 years ago
1
Chapter 14 1 Chapter 14 Storing and Retrieving XML in SQL Server 2000 November 6, 2001 Sook-Kyo Kwon
2
Chapter 14 2 Contents Objectives Retrieving XML : FOR XML Storing XML : OPENXML XML Bulk Load Summary
3
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
4
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.
5
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]
6
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
7
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”>
8
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 …… …… ……
9
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 10643 1997-08-25T00:00:00 ….. ANATR Ana Trujillo 10308 1996-09-18T00:00:00 ……
10
Chapter 14 10 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”
11
Chapter 14 11 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
12
Chapter 14 12 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”> …
13
Chapter 14 13 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 …
14
Chapter 14 14 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 --------------------------------------------------------------------------------------------- 1NULL“ALFKI”“Maria Andrers” 1NULL“ANATR”“Ana Trujillo” 1NULL“ANTON”“Antonio Moreno” > …
15
Chapter 14 15 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 --------------------------------------------------------------------------------------------- 1NULLALFKINULL 1NULLANATRNULL TagParent Customer!1!CustomerIDOrder!2!OrderID --------------------------------------------------------------------------------------------- 21ALFKI10643 21ALFKI10692 21ANATR10308 21ANATR10625 SELECT 2, 1, Customers.CustomerID Orders.OrderID FROMCustomers,Orders WHERECustomers.CustomerID = Orders.CustomerID
16
Chapter 14 16 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 ----------------------------------------------------------------------------- 1NULL ALFKINULL 21 ALFKI10643 21 ALFKI10692 21 ALFKI… 1NULL ANATRNULL 21 ANATR10308 21 ANATR10625 21 ANATR…..
17
Chapter 14 17 Retrieving XML from SQL server 2000: FOR XML (14) The EXPLICIT Mode (8) Hierarchy Generation – Union All:Resulting XML …… …… ……
18
Chapter 14 18 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
19
Chapter 14 19 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]
20
Chapter 14 20 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 (@hdoc, ‘/root/Customer/Order’) WITH CustOder The document handle and the table name are passed to OPENXML
21
Chapter 14 21 Storing XML in SQL server 2000: OPENXML (4) oidorderdaterequireddate -------------------------------------------------------------------------------------- Ord12000-10-01 00:00:00.0002000-11-01 00:00:00.000 Ord22000-10-02 00:00:00.0002000-12-01 00:00:00.000 Ord32000-09-01 00:00:00.0002000-10-01 00:00:00.000 Ord42000-09-02 00:00:00.0002000-10-02 00:00:00.000 INSERT INTO CustOrder SELECT * FROM OPENXML (@hdoc, ‘/root/Customer/Order’) WITH CustOder The resulting three-column rowset returned by the SELECT statement UPDATE CustOrder SET requireddate = (SELECT requireddate FROM OPENXML (@hdoc, ‘/root/Customer/Order’) WITH CustOrder WHERE oid = ‘Oid1’) SELECT * FROM OPENXML (@hdoc, ‘/root/Customer/Order’) WITH (oidvarchar(20) orderdatedatetime orderdatedatetime requireddatedatetime) requireddatedatetime) Specify rowset schema
22
Chapter 14 22 Storing XML in SQL server 2000: OPENXML (5) Attribut-Centric and Element-centric Mapping DECLARE @hdoc int DECLARE @doc varchar(1000) SET @doc=‘ 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 sp_xml_prepareddocument @hdoc OUTPUT, @doc EXEC sp_xml_removeddocument @hdoc SELECT * FROM OPENXML (@hdoc, ‘/root/Customer/Order’,3) WITH (oidvarchar(20) orderdatedatetime orderdatedatetime requireddatedatetime) requireddatedatetime)
23
Chapter 14 23 Storing XML in SQL server 2000: OPENXML (6) OrdIDOrdDateOrdReqDate -------------------------------------------------------------------------------------- Ord12000-10-01 00:00:00.0002000-11-01 00:00:00.000 Ord22000-10-02 00:00:00.0002000-12-01 00:00:00.000 Ord32000-09-01 00:00:00.0002000-10-01 00:00:00.000 Ord42000-09-02 00:00:00.0002000-10-02 00:00:00.000 Additional Mapping Information : genral syntax ColumnName datatpe [AdditonalMapping] SELECT * FROM OPENXML (@hdoc, ‘/root/Customer/Order’,1) WITH (OrdIDvarchar(20) ‘@oid’, OrdDatedatetime ‘@orderdate’, OrdDatedatetime ‘@orderdate’, OrdReqDatedatetime ‘@requireddate’) OrdReqDatedatetime ‘@requireddate’)
24
Chapter 14 24 Storing XML in SQL server 2000: OPENXML (7) oidCustNameUniqueIDValNodeNameNodeSibling ------------------------------------------------------------------------------------------ Ord1Bob6OrderNULL Ord2Bob12Order6 Ord3John21OrderNULL Ord4John26Order21 Metaproperty Attributes - @mp:id, @mp:localname, @mp:namespaceuri, @mp:prefix, @mp:prev, @mp:xmltext SELECT * FROM OPENXML (@hdoc, ‘/root/Customer/Order’,3) WITH ( oid varchar(20) CustName varchar(10) ‘../@name’, CustName varchar(10) ‘../@name’, UniqueIDVal int ‘mp@:id’, UniqueIDVal int ‘mp@:id’, NodeNmae varchar(10) ‘mp@localname’, NodeSibiling varchar(10) ‘mp:prev’)
25
Chapter 14 25 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
26
Chapter 14 26 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 ‘2000-01-01’) Go XML Data XML Data is bulk loaded in these tables
27
Chapter 14 27 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.