SQL Server 2005中带有“FOR XML PATH”的XML声明

时间:2010-01-21 23:57:55

标签: sql xml sql-server-2005 for-xml-path

以下是我已创建的查询的简化版本。查询工作正常,但我无法弄清楚如何在生成的XML的顶部获取XML声明。我尝试了多种方法,并在谷歌上进行了广泛的搜索,但唉,我似乎无法找到如何做到这一点......或者即使有可能。

select 
    'Dimension' "@type",
    (
        select
            (
                select
                    'X102' "TransactionType",
                    convert(varchar, getdate(), 104) "Transfer/TransferDate",
                    convert(varchar, getdate(), 108) "Transfer/TransferTime"
                for xml path (''), type
            ) "TransactionInformation"
        for xml path (''), type
    )
for xml path ('DimensionImport'), type

给我......

<DimensionImport type="Dimension">
    <TransactionInformation>
        <TransactionType>X102</TransactionType>
        <Transfer>
            <TransferDate>21.01.2010</TransferDate>
            <TransferTime>15:46:36</TransferTime>
        </Transfer>
    </TransactionInformation>
</DimensionImport>

我想要......

<?xml version="1.0" encoding="ISO-8859-1" ?>
<DimensionImport type="Dimension">
    <TransactionInformation>
        <TransactionType>X102</TransactionType>
        <Transfer>
            <TransferDate>21.01.2010</TransferDate>
            <TransferTime>15:46:36</TransferTime>
        </Transfer>
    </TransactionInformation>
</DimensionImport>

提前感谢您提供任何可以借出的帮助。

5 个答案:

答案 0 :(得分:3)

它很乱,但你可以在前面连接它......

SELECT '<? xml...>' + 
(select 
    'Dimension' "@type",
    (
        select
            (
                select
                    'X102' "TransactionType",
                    convert(varchar, getdate(), 104) "Transfer/TransferDate",
                    convert(varchar, getdate(), 108) "Transfer/TransferTime"
                for xml path (''), type
            ) "TransactionInformation"
        for xml path (''), type
    )
for xml path ('DimensionImport'), type)

答案 1 :(得分:2)

试试这个:

select '<?xml version="1.0" encoding="ISO-8859-1" ?>' + 
       (your whole upper select here)

答案 2 :(得分:1)

不幸的是,这是我在SQL Server Books Online中找到的:

当实例存储在数据库中时,不保留实例中的XML声明PI。例如:

Copy Code CREATE TABLE T1 (Col1 int primary key, Col2 xml)
GO
INSERT INTO T1 values (1, '<?xml version="1.0" encoding="windows-1252" ?><doc></doc>')
GO
SELECT Col2
FROM T1

结果是<doc/>

唯一的解决方法是将XML作为varchar(max)类型返回:

select '<?xml version="1.0" encoding="ISO-8859-1" ?>'
+
cast( (
select 
    'Dimension' "@type",
    (
        select
            (
                select
                    'X102' "TransactionType",
                    convert(varchar, getdate(), 104) "Transfer/TransferDate",
                    convert(varchar, getdate(), 108) "Transfer/TransferTime"
                for xml path (''), type
            ) "TransactionInformation"
        for xml path (''), type
    )
for xml path ('DimensionImport'), type) as varchar(max))

答案 3 :(得分:1)

这是我最终做的事情。数据类型不作为XML数据类型返回,但我可以处理这个问题。

select '<?xml version="1.0" encoding="ISO-8859-1" ?>' +
(
    select 
        'Dimension' "@type",
        (
            select
                (
                    select
                        'X102' "TransactionType",
                        convert(varchar, getdate(), 104) "Transfer/TransferDate",
                        convert(varchar, getdate(), 108) "Transfer/TransferTime"
                    for xml path (''), type
                )
            for xml path ('TransactionInformation'), type
        ),
        (
            ... queried up data here ...
        )
    for xml path ('DimensionImport')
)

答案 4 :(得分:0)

2008年来自EM的R2,如果你打开“另存为”并展开“保存”按钮,那么你可以保存女巫编码,之后,自动添加xml声明。

希望它有所帮助。 问候。