SQL Server - Importar XML a SQLServer

 
Vista:
sin imagen de perfil
Val: 55
Ha mantenido su posición en SQL Server (en relación al último mes)
Gráfica de SQL Server

Importar XML a SQLServer

Publicado por ANTONIO (28 intervenciones) el 28/10/2020 14:32:51
Buenas tengo un XML con el que tengo que poblar la Base de Datos cuyo contenido es el que adjunto. Como se puede ver hay diferentes niveles de nodos.
Aunque es la primera vez que hago esta operación me he hecho el siguiente codigo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
CREATE TABLE PRUEBA(
    ContadorCierre int not null,
	CodMaquina int not null,
	FechaCierre date not null,
	HoraCierre time not null,
	CodUsuario int not null,
	CodClienteIsis int not null,
	FechaTraspaso date not null,
	HoraTraspaso time not null,
	ContadorLiq int not null,
	CodConductor int not null,
	FechaLiquidacion date not null,
	HoraLiquidacion time not null,
	Recaudacion int not null,
	Codigo int ,
	Posicion int not null,
	Respuestas int not null,
	Fecha date not null,
	Hora time not null
)
GO
 
INSERT INTO PRUEBA (ContadorCierre,CodMaquina,FechaCierre,HoraCierre,CodUsuario,CodClienteIsis,FechaTraspaso,HoraTraspaso,
		ContadorLiq,CodConductor,FechaLiquidacion,HoraLiquidacion,Recaudacion,
		Codigo,Posicion,Respuestas,Fecha,Hora)
SELECT
   MY_XML.DatosRC.query('ContadorCierre').value('.', 'INT') AS ContadorCierre,
   MY_XML.DatosRC.query('CodMaquina').value('.', 'INT') AS CodMaquina,
   MY_XML.DatosRC.query('FechaCierre').value('.', 'date') AS FechaCierre,
   MY_XML.DatosRC.query('HoraCierre').value('.', 'time') AS HoraCierre,
   MY_XML.DatosRC.query('CodUsuario').value('.', 'INT') AS CodUsuario,
   MY_XML.DatosRC.query('CodClienteIsis').value('.', 'INT') AS CodClienteIsis,
   MY_XML.DatosRC.query('FechaTraspaso').value('.', 'date') AS FechaTraspaso,
   MY_XML.DatosRC.query('HoraTraspaso').value('.', 'time') AS HoraTraspaso,
   MY_XML.DatosRC.query('ContadorLiq').value('.', 'INT') AS ContadorLiq,
   MY_XML.DatosRC.query('CodConductor').value('.', 'INT') AS CodConductor,
   MY_XML.DatosRC.query('FechaLiquidacion').value('.', 'date') AS FechaLiquidacion,
   MY_XML.DatosRC.query('HoraLiquidacion').value('.', 'time') AS HoraLiquidacion,
   MY_XML.DatosRC.query('Recaudacion').value('.', 'INT') AS Recaudacion,
   MY_XML.DatosRC.query('Codigo').value('.', 'INT') AS Codigo,
   MY_XML.DatosRC.query('Posicion').value('.', 'INT') AS Posicion,
   MY_XML.DatosRC.query('Respuesta').value('.', 'INT') AS Respuesta,
   MY_XML.DatosRC.query('Fecha').value('.', 'date') AS Fecha,
   MY_XML.DatosRC.query('Hora').value('.', 'time') AS Hora
 
 
FROM (SELECT CAST(MY_XML AS xml)
      FROM OPENROWSET(BULK 'C:\···· SERVER-25 ····\Programacion\Mantenimiento [Repaso] (Programado-Realizado)\RC_CIE_9823_00768_20201001000023.XML', SINGLE_BLOB) AS T(MY_XML)) AS T(MY_XML)
      CROSS APPLY MY_XML.nodes('DatosRC/Cierre/ListaLiquidaciones/Liquidacion/Calidad/Pregunta') AS MY_XML (DatosRC)
;

pero en este caso solo me trae los datos del nivel más bajo (5 ultimos campos), del resto nada (a cero), y si omito estos ultimos si me trae bien los niveles más altos, he intentado otras técnicas como meter el root, por ejemplo :
FechaLiquidacion = resource.value('(../../Liquidacion/FechaLiquidacion)[1]', 'varchar(255)'), pero entiendo que algo me falta para poder enlazar los nodos
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder