viernes, 18 de febrero de 2011

OpenXML


Convierte un XMl a una Tabla

DECLARE @xml_text VARCHAR(4000), @i INT

SELECT @xml_text = '<root><person LastName="White" FirstName="Johnson"/>
<person LastName="Green" FirstName="Marjorie"/>
<person LastName="Carson" FirstName="Cheryl"/></root>'

EXEC sp_xml_preparedocument @i OUTPUT, @xml_text

SELECT * FROM
    OPENXML(@i, '/root/person') WITH (LastName nvarchar(50),
                        FirstName nvarchar(50))

Convertir un XML a una Tabla en SQL Server 2008

declare @mydoc xml
set @mydoc='<Orders> 
<Order OrderID="13000" CustomerID="ALFKI" OrderDate="2006-09-20Z"
EmployeeID="2">
</Order> 
<Order OrderID="13001" CustomerID="VINET" OrderDate="2006-09-20Z"
EmployeeID="1">
</Order>
</Orders>'

SELECT OrderID    = T.Item.value('@OrderID', 'int'),      
CustomerID = T.Item.value('@CustomerID', 'nchar(5)'),      
OrderDate  = T.Item.value('@OrderDate',  'datetime'),      
EmployeeId = T.Item.value('@EmployeeID', 'smallint')
FROM   @mydoc.nodes('Orders/Order') AS T(Item)