Return to Snippet

Revision: 66558
at May 23, 2014 06:34 by rralhan


Initial Code
-- FIRST WAY

Declare @xml as xml;

set @xml =
 '<LandLord xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Address1>1092 Bel Lido Dr</Address1> 
  <City>Highland Beach</City> 
  <County>Palm Beach County</County> 
  <State>FL</State> 
  <Zip>33487</Zip> 
  <DispStreetView>true</DispStreetView> 
  <UserId>271111</UserId> 
  <PropertyId>1234</PropertyId> 
  </LandLord>'


 SELECT  
       Tbl.Col.value('Address1[1]', 'varchar(100)') as address1,  
       Tbl.Col.value('City[1]', 'varchar(100)') as City,  
       Tbl.Col.value('County[1]', 'varchar(100)') as County,
       Tbl.Col.value('State[1]', 'varchar(100)') as State,
       Tbl.Col.value('DispStreetView[1]', 'bit') as DisplayStreetView,
       Tbl.Col.value('UserId[1]', 'int') as UserId
FROM   @xml.nodes('//LandLord') Tbl(Col)   


-- SECOND WAY


DECLARE @idoc int;
DECLARE @doc varchar(1000);
SET @doc ='
<ROOT>
<Customer CustomerID="VINET" ContactName="Paul Henriot">
   <Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00">
      <OrderDetail OrderID="10248" ProductID="11" Quantity="12"/>
      <OrderDetail OrderID="10248" ProductID="42" Quantity="10"/>
   </Order>
</Customer>
<Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
   <Order CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00">
      <OrderDetail OrderID="10283" ProductID="72" Quantity="3"/>
   </Order>
</Customer>
</ROOT>'
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT    *
FROM       OPENXML (@idoc, '/ROOT/Customer',1)
			WITH (CustomerID  varchar(10),
			      ContactName varchar(20))

EXEC sp_xml_removedocument @idoc

Initial URL

                                

Initial Description
Parse xml (passed as a parameter) in SQL

Initial Title
Parse xml in SQL

Initial Tags
sql, xml

Initial Language
SQL