viernes, 18 de julio de 2008

Manejando XML en SQLServer 2005

SQLServer 2005 incorpora nuevas funcionalidades para manejar sql server. Una de ellas es el manejo de xml. Dejo aca un ejemplo de manemplo de ello que se puede ejecutar con el SQL server management studio. El xml lo saque de www.w3schools.com y lo recorte por si a alguno le resulta similar

declare @xml as xml


set @xml =
'<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>

<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>

<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>

<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
</catalog>';



select @xml.query('/catalog/cd/title')

SELECT
coleccion.cd.value('title[1]','VARCHAR(100)'),
coleccion.cd.value('artist[1]','VARCHAR(100)'),
coleccion.cd.value('country[1]','VARCHAR(100)'),
coleccion.cd.value('company[1]','VARCHAR(100)'),
coleccion.cd.value('price[1]','VARCHAR(100)'),
coleccion.cd.value('year[1]','VARCHAR(100)')
FROM
@xml.nodes('/catalog/cd') as coleccion(cd)

No hay comentarios: