Ver Mensaje Individual
  #2 (permalink)  
Antiguo 13/10/2008, 09:41
Avatar de matanga
matanga
 
Fecha de Ingreso: octubre-2007
Ubicación: España
Mensajes: 1.091
Antigüedad: 16 años, 6 meses
Puntos: 85
Respuesta: XML DB Oracle, Sql Server, MySQL

Motor: Sql Server
Versión Mínima: Sql Server 2005
Fuente:http://msdn.microsoft.com/en-us/library/ms345117.aspx http://msdn.microsoft.com/en-us/libr...7(SQL.90).aspx

1. Fichero XML

Código:
D:\xml>dir
 Volume in drive D has no label.
 Volume Serial Number is CE41-1E7C

 Directory of D:\xml

13/10/2008  12:07    <DIR>          .
13/10/2008  12:07    <DIR>          ..
13/10/2008  12:11               445 data.xml
               1 File(s)            445 bytes
               2 Dir(s)   5.093.183.488 bytes free
2. Tabla XML

Código:
create table t1
(id numeric(8),
xml_data XML)
go
3. Carga del fichero XML via SQL

Código:
insert into t1
select 1, (select * from openrowset ( bulk 'D:\xml\data.xml', single_clob) as xmlData)
go

1> select * from t1
2> go
id         xml_data

         1 <root><entity><id>1</id><f_name>nombre_1</f_name><l_name>apellido_1</
l_name><dni>dni_1</dni></entity><entity><id>2</id><f_name>nombre_2</f_name><l_na
me>apellido_2</l_name><dni>dni_2</dni></entity><entity><id>3</id><f_name>nombre_
3</f_name><l_name>apellido_

(1 rows affected)
4. Ejemplos de XQuery y XPath

Código:
1> select
2> id,
3> xml_data.query('/root//entity[1]//f_name/text()') as data
4> from t1
5> go
id         data

         1 nombre_1


(1 rows affected)
5. Función Exists

Código:
1> select count(*)
2> from t1
3> where xml_data.exist('/root//entity[1]//f_name')  = 1
4> go

          1

(1 rows affected)
6. Actualizar datos

Código:
update t1
set xml_data.modify 
('replace value of (/root//entity//l_name/text())[1] with "apellido_11"')
Go

1> select
2> id,
3> xml_data.query('/root//entity[1]//l_name/text()') as data
4> from t1
5> go
id         data

         1 apellido_11

(1 rows affected)
7. Transformar una tabla a XML

Código:
create table t2
(id numeric(8),
f_name varchar(30),
l_name varchar(30),
dni varchar(30)
)
Go

insert into t2 values (1,'nombre_1','apellido_1','dni_1')
Go
insert into t2 values (2,'nombre_2','apellido_2','dni_2')
Go
insert into t2 values (3,'nombre_3','apellido_3','dni_3')
Go
7.1 Valores como atributos

Código:
select * from t2 as Entity
for xml auto
Go

<Entity id="1" f_name="nombre_1" l_name="apellido_1" dni="dni_1" />
<Entity id="2" f_name="nombre_2" l_name="apellido_2" dni="dni_2" />
<Entity id="3" f_name="nombre_3" l_name="apellido_3" dni="dni_3" />
7.2 Valores como Elementos

Código:
select id, f_name, l_name, dni
from t2
for xml path('Entity'), root('root')
Go

<root>
  <Entity>
    <id>1</id>
    <f_name>nombre_1</f_name>
    <l_name>apellido_1</l_name>
    <dni>dni_1</dni>
  </Entity>
  <Entity>
    <id>2</id>
    <f_name>nombre_2</f_name>
    <l_name>apellido_2</l_name>
    <dni>dni_2</dni>
  </Entity>
  <Entity>
    <id>3</id>
    <f_name>nombre_3</f_name>
    <l_name>apellido_3</l_name>
    <dni>dni_3</dni>
  </Entity>
</root>