如何在XML查询的SELECT中选择返回的列名?

时间:2022-10-26 20:22:06

MS SQL has a convenient workaround for concatenating a column value from multiple rows into one value:

MS SQL可以方便地将多个行中的列值连接到一个值:

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

and that returns a nice recordset:

它返回一个很好的记录集:

XML_F52E2B61-18A1-11d1-B105-00805F49916B                                     
---------------------------------------- 
<col1>Foo</col1><col1>Bar</col1>

only the column name in the returned recordset is rather nasty!

只有返回的记录集中的列名是相当讨厌的!

The column name seems to include random elements (or a GUID), and hence I am reluctant to use it in my application (different instances or different servers might have another GUID). Unfortunately I cannot use * to select the value, and due to the restrictions in the existing application I cannot iterate through returned columns, either...

列名似乎包含随机元素(或GUID),因此我不愿意在应用程序中使用它(不同的实例或不同的服务器可能有另一个GUID)。不幸的是,我不能使用*来选择值,而且由于现有应用程序中的限制,我也不能遍历返回的列……

Is there a way to force the column name in the returned recordset to something more sensible?

是否有一种方法可以将返回的记录集中的列名强制为更合理的内容?

6 个解决方案

#1


67  

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

不漂亮,但应该给出你需要的结果

#2


11  

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.

注意:如果在“for xml”后面省略了“type”,就会得到(我认为)一个字符串。

#3


2  

stored procedure

存储过程

declare @requestResultXML xml

set @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

#4


0  

For EXPLICIT xml generation - with unions you need to wrap results one more time (As a bonus result as XML):

对于显式xml生成——使用联合,您需要再次包装结果(作为xml的附加结果):

SELECT 
    CAST(  
        (
            SELECT 
                * 
            FROM (
                SELECT 
                    1 AS Tag
                    ,NULL AS Parent
                    ...
                UNION ALL
                SELECT ...
                FOR XML EXPLICIT
            )
        ) as XML) as [MyName]

#5


0  

DECLARE @XmlData XML;

SET @XmlData = (
        SELECT *
        FROM [dbo].[TABLE1]
        FOR XML PATH('ChildNodeDetailsResponse')
            ,ROOT('ParentNode')
        )

SELECT @XmlData AS Result

#6


0  

DECLARE @XmlData XML;
SET @XmlData =(SELECT * FROM [dbo].[Users] ORDER by UserName FOR XML path(''))
SELECT @XmlData AS Result

#1


67  

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

不漂亮,但应该给出你需要的结果

#2


11  

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.

注意:如果在“for xml”后面省略了“type”,就会得到(我认为)一个字符串。

#3


2  

stored procedure

存储过程

declare @requestResultXML xml

set @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

#4


0  

For EXPLICIT xml generation - with unions you need to wrap results one more time (As a bonus result as XML):

对于显式xml生成——使用联合,您需要再次包装结果(作为xml的附加结果):

SELECT 
    CAST(  
        (
            SELECT 
                * 
            FROM (
                SELECT 
                    1 AS Tag
                    ,NULL AS Parent
                    ...
                UNION ALL
                SELECT ...
                FOR XML EXPLICIT
            )
        ) as XML) as [MyName]

#5


0  

DECLARE @XmlData XML;

SET @XmlData = (
        SELECT *
        FROM [dbo].[TABLE1]
        FOR XML PATH('ChildNodeDetailsResponse')
            ,ROOT('ParentNode')
        )

SELECT @XmlData AS Result

#6


0  

DECLARE @XmlData XML;
SET @XmlData =(SELECT * FROM [dbo].[Users] ORDER by UserName FOR XML path(''))
SELECT @XmlData AS Result