programming4us
programming4us
DATABASE

Programming Microsoft SQL Server 2005 : FOR XML Commands (part 3) - OPENXML Enhancements in SQL Server 2005

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

OPENXML Enhancements in SQL Server 2005

Up until now, we have been composing XML from rows of data, but what if we already have XML data and we want to shred it back into relational data? Well, SQL Server 2000 introduced a feature for this purpose called OPENXML. OPENXML is a system function that allows an XML document to be shredded into T-SQL rows. SQL Server 2005 also has the OPENXML function—with some enhancements, of course.

To shred data into relational rows using OPENXML, you must first create an XML document handle using the system stored procedure sp_xml_preparedocument. This system stored procedure takes an XML document and creates a representation that is referenced via a handle, which it returns via an OUTPUT parameter. OPENXML uses this handle along with a specified path and behaves like a database view to the XML data, so you simply choose SELECT from the OPENXMLSELECT from a table or a view. The following code shows an example of OPENXML in action. function just as you would

USE AdventureWorks

DECLARE @int int
DECLARE @xmlORDER varchar(1000)
SET @xmlORDER ='
<ROOT>
<Customer CustomerID="BRU" ContactName="Andrew Brust">
<Order CustomerID="BRU" EmployeeID="5" OrderDate="2005-11-04">
<OrderDetail OrderID="10248" ProductID="16" Quantity="12"/>
<OrderDetail OrderID="10248" ProductID="32" Quantity="10"/>
</Order>
</Customer>
<Customer CustomerID="ZAC" ContactName="Bill Zack">
<Order CustomerID="ZAc" EmployeeID="3" OrderDate="2005-11-16">
<OrderDetail OrderID="10283" ProductID="99" Quantity="3"/>
</Order>
</Customer>
</ROOT>'
--Create an internal representation of the XML doc
EXEC sp_xml_preparedocument @int OUTPUT, @xmlORDER
-- OPENXML rowset provider.
SELECT *
FROM OPENXML (@int, '/ROOT/Customer',1)
WITH (CustomerID varchar(10),ContactName varchar(20))


The code here takes the XML text and allows you to query and work with it as if it were relational data. The output looks like this:

CustomerID     ContactName
---------------------------
BRU Andrew Brust
ZAC Bill Zack
(2 row(s) affected)

You can optionally specify if you want OPENXML to use element-based or attribute-based XML relational mapping between the rowset columns and the XML nodes. There are two ways to control the mapping. The first is to use the flags parameter, which assumes that the XML nodes will map to corresponding rowset columns with exactly the same name. You can also use the ColPattern parameter, an XPath expression that allows you to use a schema to perform the mapping as part of SchemaDeclaration in the WITH clause. The mapping specified in ColPattern overwrites the mapping specified by the flags parameter.

SQL Server 2005 introduces two enhancements to OPENXML, both involving the new XML data type. First, the XML data type is supported as an output column or an overflow column with the OPENXML statement. Second, you can pass an XML data type variable directly into sp_xml_preparedocument. Both of these enhancements enable you to more easily work with existing XML data in an XML column or created data using FOR XML TYPE.

XML Bulk Load

SQL Server 2000 XML Bulk Load allows users to load large XML documents on the client side. It works by reading the XML and producing SQL INSERTXML data type column. statements that run on the client in batch. SQL Server 2005 greatly enhances XML Bulk Load by allowing it to run on the server as well as to load directly into an

Using the new XML Bulk Load requires using the system rowset provider function OPENROWSET and specifying the BULK provider:

Use AdventureWorks
--create a table with an xml column
create table tblxmlcustomers
(customer_id int primary key identity,
customer_xml xml not null)

--this file will load 1 record in (SINGLE_CLOB)
--for more records use a format file
insert into tblxmlcustomers
Select * from OPENROWSET
(Bulk 'C:\customer_01.xml',
SINGLE_CLOB) as xmldata

This example works by first creating a table that has an XML column and a primary key value. Then we use an INSERT statement that selects all of the data from the XML file using OPENROWSET. OPENROWSET uses the BULK provider and loads into the XML column of the tblxmlcustomers table the entire contents of the customer_01.xml file.

Other  
  •  SQL Server 2008 : Audit-Related Startup and Shutdown Problems
  •  SQL Server 2008 : Creating SQL Server Audits Using the GUI
  •  SQL Server 2008 : Creating Database Audit Specifications
  •  Programming Microsoft SQL Server 2005 : The XML Data Type (part 3) - XML Indexes
  •  Programming Microsoft SQL Server 2005 : The XML Data Type (part 2) - XML Schemas
  •  Programming Microsoft SQL Server 2005 : The XML Data Type (part 1) - Working with the XML Data Type as a Variable & Working with XML in Tables
  •  SQL Server 2008 : Auditing SQL Server - Creating Server Audit Specifications
  •  SQL Server 2008 : Auditing SQL Server - Creating SQL Server Audits with T-SQL
  •  Programming Microsoft SQL Serve 2005 : An Overview of SQL CLR - Security
  •  Programming Microsoft SQL Serve 2005 : An Overview of SQL CLR - CLR Aggregates
  •  
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us