Google Groups Home
Help | Sign in
de XML a Tabla SQL
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Aldo  
View profile
 More options Jul 4, 12:26 pm
Newsgroups: microsoft.public.es.sqlserver
From: Aldo <ateixei...@hotmail.com>
Date: Fri, 4 Jul 2008 09:26:17 -0700 (PDT)
Local: Fri, Jul 4 2008 12:26 pm
Subject: de XML a Tabla SQL
Hola a todos y gracias de antemano,
Mi problema es el siguiente:
1. Tengo la siguiente variable XML:
--------------------------------------------------------------------------- -----
DECLARE @TEMPORAL xml
SET @TEMPORAL = '<Raiz>
<Registro>
<Campo1>1</Campo1>
<Campo2>MI DATO</Campo2>
<Campo3>15.62</Campo3>
<Campo4>2008/12/31</Campo4>
<Campo5>13.996</Campo5>
</Registro>
<Registro>
<Campo1>2</Campo1>
<Campo2>MI SEGUNDO DATO</Campo2>
<Campo3>70.62</Campo3>
<Campo4>2008/01/27</Campo4>
<Campo5>1852.996</Campo5>
</Registro>
</Raiz>'

2. Puedo saber la cantidad de Registros que tengo, con esta
instruccion y hasta puedo almacenar el total de registros en una
variable entera:
--------------------------------------------------------------------------- -------------------------
SELECT CONVERT(int,CONVERT(varchar,@TEMPORAL.query('count(/Raiz/
Registro)') ))

3. Con la siguiente instrucción capturo el primer registro:
--------------------------------------------------------------------------- -----------------------------------------
SELECT CONVERT(int,CONVERT(varchar,@TEMPORAL.query('/Raiz/Registro[1]/
Campo1/node()'))),
CONVERT(varchar,@TEMPORAL.query('/Raiz/Registro[1]/Campo2/node()')),
CONVERT(smallmoney,CONVERT(varchar,@TEMPORAL.query('/Raiz/Registro[1]/
Campo3/node()'))),
CONVERT(smalldatetime,CONVERT(varchar,@TEMPORAL.query('/Raiz/
Registro[1]/Campo4/node()'))),
CONVERT(real,CONVERT(varchar,@TEMPORAL.query('/Raiz/Registro[1]/Campo5/
node()')))

4. El problema es para seleccionar de manera dinámica un nro de
registro, es decir si tengo 20 registros, quisiera acceder al registro
n. donde n puede ser un numero del 1 al 20. He intentado resolver
haciendo una concatenacion en la frase del @TEMPORAL.query de la
siguiente manera: '/Raiz/Registro[' + CONVERT(varchar, n) + ']/Campo5/
node()'; pero no me acepta , me sale el siguiente error:
--------------------------------------------------------------------------- ------------------------------------------
Msg 8172, Level 16, State 1, Line 20
The argument 1 of the xml data type method "query" must be a string
literal.

Muchas gracias


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jhonny Vargas P.  
View profile
 More options Jul 4, 1:09 pm
Newsgroups: microsoft.public.es.sqlserver
From: "Jhonny Vargas P." <c_h_a_n_g_e_sREM...@hotmail.com>
Date: Fri, 4 Jul 2008 13:09:28 -0400
Local: Fri, Jul 4 2008 1:09 pm
Subject: Re: de XML a Tabla SQL
Ufffff... que lento... hice una prueba y fue demasiado lento...

Probé con esto y mejoró muchísimo...

DECLARE @idoc INT

DECLARE @TEMPORAL xml
SET @TEMPORAL = '<Raiz>
<Registro>
<Campo1>1</Campo1>
<Campo2>MI DATO</Campo2>
<Campo3>15.62</Campo3>
<Campo4>2008/12/31</Campo4>
<Campo5>13.996</Campo5>
</Registro>
<Registro>
<Campo1>2</Campo1>
<Campo2>MI SEGUNDO DATO</Campo2>
<Campo3>70.62</Campo3>
<Campo4>2008/01/27</Campo4>
<Campo5>1852.996</Campo5>
</Registro>
</Raiz>'

EXEC sp_xml_preparedocument @idoc OUTPUT, @TEMPORAL

-- Contamos cuantos registros son
SELECT   COUNT(1)
FROM OPENXML (@idoc, '/Raiz/Registro',2)
WITH  ( Campo1 INT, Campo2 VARCHAR(100))

-- Ahora devuelvo el registro "n"...
DECLARE @REGISTRO VARCHAR(1000)
DECLARE @n INT

SET @n = 1
SET @REGISTRO = '/Raiz/Registro[' + CAST(@n as VARCHAR(5)) + ']'

SELECT   *
FROM OPENXML (@idoc, @REGISTRO ,2)
WITH  ( Campo1 INT, Campo2 VARCHAR(100))

EXEC sp_xml_removedocument @idoc

Espero te sirva.

Saludos,
Jhonny Vargas P.

"Aldo" <ateixei...@hotmail.com> escribió en el mensaje de
noticias:7961d29d-fcc3-433a-8161-f5bbd578c...@x35g2000hsb.googlegroups.com...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google