1

I have this SQL and it works:

set nocount on 

DECLARE
    @OrderID VARCHAR(200) = '13095314100';

declare @eOrder Table
(
  colOrderId  varchar(20),
  colDeliveryCountryCode varchar(20)
 )

insert into @eOrder values ('13095314100', 'SE')


SELECT 
    'name'  AS 'Val/@n', Sender.name AS Val

FROM @eOrder E
  inner join 
    (
       select  '12345' SenderID,     'KappAhl Sverige AB' "name",  'Idrottsvägen 14' "address1",   null address2,       '431 24' ZipCode,   'MÖLNDAL' City, 'SE' Country,  '010-138 87 11' phone
       union
       select  '12345',          'KappAhl Shop Online',       'c/o KappAhl OY',             'Unikkotie 3 C',    '01300'     ,  'VANTAA',        'PL',        '0753 267 881'    
    ) Sender
    on E.colDeliveryCountryCode = Sender.Country

WHERE colOrderId = @OrderID
FOR XML PATH('Sender');

I get this output as expected:

<Sender><Val n="name">KappAhl Sverige AB</Val></Sender>

But how do I add SenderID as an attribute to my Sender tag?

Wanted XML output:

<Sender SenderID="12345" ><Val n="name">KappAhl Sverige AB</Val></Sender>

1 Answer 1

2

Add Sender.SenderID as '@SenderID' to your select:

SELECT Sender.SenderID as '@SenderID',
    'name'  AS 'Val/@n', Sender.name AS Val

Result:

enter image description here

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.