martes, 21 de septiembre de 2010

Resultado XML de un select dinamico

-Quiere obtener el resultado de un select dinamico? no puede hacerlo? :D y ademas quiere sacar un xml como salida?
-Aqui esta el ejemplo que usted buscaba... (probado en sql server 2008).

Saludos cordiales

- Want to get the result of a dynamic select? can not do? : D and also wants to get an xml as output?
- Here is the example you wanted ... (Tested in SQL Server 2008).

Best regards



--I make a temporal table
--Creo una tabla temporal
CREATE TABLE TEMPORALTBL (
ID INT,
DESCRIPTION varchar(200)
)

--Insert the example rows
--Inserto registros de ejemplo
INSERT INTO TEMPORALTBL VALUES (1,'THIS IS A TEST')
INSERT INTO TEMPORALTBL VALUES (2,'MAY ENGLISH IS POOR')
INSERT INTO TEMPORALTBL VALUES (3,'MAY SPANISH IS THE POOREST')


DECLARE @cmd NVARCHAR(2000)
DECLARE @result XML

--My dynamic select
--Mi select dinamico
SET @cmd = N'select top 100 * from temporaltbl '

--I make a cast to xml
--Hago un cast a xml
SET @cmd = N'select @insideresult= CAST((' +@cmd + ' FOR XML AUTO, Elements ) AS xml) '

--EXECUTE THE SELECT!!!! CAN YOU SEE?
--EJECUTO EL SELECT... CABRON LO PUEDES VER?
EXEC sp_executeSQL @cmd ,N'@insideresult xml output',@insideresult = @result OUTPUT

--Show the XML and command
--Muestro el resultado del xml y el comandojavascript:void(0)
SELECT @cmd as command, @result as Textxml

--Drop the example table
--Borro la tabla de ejemplo
DROP TABLE temporaltbl
/*
Compatible SQL 2008
SQL 2008 compatible
*/

No hay comentarios:

Publicar un comentario