Friday, March 9, 2012

How to concat two xml into one xml ?

for example:
Xml1 = '<header>MyHeader</header>'
Xml2 = '<body><line>1</line><line>2</line></body>'
I want concat two xml into one xml as:
XmlResult =
'<header>MyHeader</header><body><line>1</line><line>2</line></body>'
How to concat two xmls in the sql server 2005?
You can simply cast it to varchar(max)/nvarchar(max), concatenate and then
cast it back.
declare @.x xml, @.y xml
select @.x = '<a>aaa</a>', @.y='<b>bbb</b>'
select cast(cast(@.x as nvarchar(max)) + cast(@.y as nvarchar(max)) as xml)
Uytkownik "ABC" <abc@.abc.com> napisa w wiadomoci
news:OOZYetDyHHA.600@.TK2MSFTNGP05.phx.gbl...
> for example:
> Xml1 = '<header>MyHeader</header>'
> Xml2 = '<body><line>1</line><line>2</line></body>'
> I want concat two xml into one xml as:
> XmlResult =
> '<header>MyHeader</header><body><line>1</line><line>2</line></body>'
> How to concat two xmls in the sql server 2005?
>
|||Pawel's method works really well but from what I remember you can also use FOR
XML.
For example, if you want to concatenate two XML variables @.x and @.y you cna do
something like
SELECT @.x, @.y FOR XML PATH ('')
Denis Ruckebusch
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Pawel Potasinski" <pawel.potasinski@.gmail.com> wrote in message
news:ebAuNkFyHHA.5980@.TK2MSFTNGP04.phx.gbl...
> You can simply cast it to varchar(max)/nvarchar(max), concatenate and then
> cast it back.
> declare @.x xml, @.y xml
> select @.x = '<a>aaa</a>', @.y='<b>bbb</b>'
> select cast(cast(@.x as nvarchar(max)) + cast(@.y as nvarchar(max)) as xml)
> Uytkownik "ABC" <abc@.abc.com> napisa w wiadomoci
> news:OOZYetDyHHA.600@.TK2MSFTNGP05.phx.gbl...
>
|||Denis,
Your solution is pretty cool and much shorter than mine. I would prefer
yours.
Thx
Regards
Pawel Potasinski
Uytkownik "Denis Ruckebusch [MSFT]" <denisruc@.online.microsoft.com> napisa
w wiadomoci news:469d55c2$1@.news.microsoft.com...
> Pawel's method works really well but from what I remember you can also use
> FOR XML.
> For example, if you want to concatenate two XML variables @.x and @.y you
> cna do something like
> SELECT @.x, @.y FOR XML PATH ('')
>
> Denis Ruckebusch
> --
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
> Use of included script samples are subject to the terms specified at
> http://www.microsoft.com/info/cpyright.htm
>
> "Pawel Potasinski" <pawel.potasinski@.gmail.com> wrote in message
> news:ebAuNkFyHHA.5980@.TK2MSFTNGP04.phx.gbl...
>

No comments:

Post a Comment