1

I am trying to generate this

<Mail>
  <Field key="SenderName" value="someone" />
  <Field key="RecipientName" value="someone else" />
<Mail/>
<Mail>
  <Field key="SenderName" value="another someone" />
  <Field key="RecipientName" value="another someone else" />
<Mail/>

heres the test data

declare @tab table (SenderName varchar(255), RecipientName varchar(255))
insert @tab
select 'someone' , 'someone else' union 
select 'another someone' , 'another someone else' 

I have tried various things with for xml path but I cant work out how to tell declare that I want a new element of the same name after recipient name.

thanks,

1 Answer 1

2
declare @tab table (SenderName varchar(255), RecipientName varchar(255))
insert @tab
select 'someone' , 'someone else' union 
select 'another someone' , 'another someone else' 

select (select 'SenderName' as "@key",
                SenderName  as "@value"
        for xml path('Field'), type),
       (select 'RecipientName' as "@key",
                RecipientName  as "@value"
        for xml path('Field'), type) 
from @tab
for xml path('Mail')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.