How to choose returned column name in a SELECT FOR XML query? How to choose returned column name in a SELECT FOR XML query? xml xml

How to choose returned column name in a SELECT FOR XML query?


That should do:

select(SELECT col1 FROM table1 WHERE col2 = 'x' ORDER by col3 FOR XML path('')) as myName

Not pretty but should give the result that you need


Try this...

select(    select '@greeting' = 'hello', '@where' = 'there', '@who' = 'world'    for xml path ('salutation'), type) as 'MyName'

Note: If you omit the "type" after the "for xml", you get (I think) a string.


stored procedure

declare @requestResultXML xmlset @requestResultXML =            (                SELECT 'NPOIT-1.0' AS '@Interface',                (                    select  'Query'     as '@Type',                            'GetBill'   as '@Query',                            'True'      as '@CompressResult'                        FOR XML PATH('Head'), TYPE                ),                (                    select  @pin        as '@PIN',                            @period     as '@Period',                            @number     as '@Number',                            @barcode    as '@Barcode'                        FOR XML PATH('QueryParams'), TYPE                )   as Data                FOR XML PATH('DataExchangeModule')                          )select @requestResultXML as GetBillRequest