0

我在存储过程中有一个查询,它检索一些 XML 格式的数据以在变量中返回@xml_data,如下所示:

SELECT @xml_data = (
    SELECT * FROM (
        SELECT   1 AS Tag
                ,0 AS Parent
                .....
        FROM    MyTable
        WHERE   id = @id

        UNION ALL

        SELECT   2 AS Tag
                ,1 AS Parent
                ....
        FROM    MyTable2
        WHERE   id = @id

        UNION ALL

        SELECT   3 AS Tag
                ,2 AS Parent
                ....
        FROM    MyTable3
        WHERE   id = @id
    ) results
FOR XML EXPLICIT, TYPE)

这就像众所周知的梦想一样工作:)

但是,我想将一个标头连接到这个 XML(例如<xml version="1.0" encoding="ISO-8859-1"/>),但不知道该怎么做。我尝试转换为NVARCHAR,在一个语句中选择两个变量,但似乎无法正确处理。

有人可以帮忙吗??

谢谢 :)

4

3 回答 3

1

尝试这样做:

DECLARE @x xml 
DECLARE @strXML varchar(MAX)
SET @x=N'<Employee><Name>Luftwaffe</Name></Employee>'
set @strXML = '<xml version="1.0" encoding="ISO-8859-1"/>' + CONVERT(varchar(MAX),@x)
SELECT @strXML  

希望能帮助到你 !

于 2012-10-08T18:02:24.610 回答
1

您可以在开头声明字符串并将它们连接在一起:

declare @xml_data nvarchar(MAX)

set @xml_data = '<xml version="1.0" encoding="ISO-8859-1"/>'

SELECT @xml_data = @xml_data +  (
    SELECT * FROM (
        SELECT   1 AS Tag
                ,0 AS Parent
                .....
        FROM    MyTable
        WHERE   id = @id

        UNION ALL

        SELECT   2 AS Tag
                ,1 AS Parent
                ....
        FROM    MyTable2
        WHERE   id = @id

        UNION ALL

        SELECT   3 AS Tag
                ,2 AS Parent
                ....
        FROM    MyTable3
        WHERE   id = @id
    ) results
FOR XML EXPLICIT, TYPE)
于 2012-10-08T18:10:58.557 回答
0

在我看来,这是最简单的方法:

declare @xml1 xml
declare @xml2 xml
declare @xml3 xml
select @xml1='<name>testname</name>'
select @xml2='<value>testvalue</value>'
select @xml3 = 
(
    select @xml1 AS xml1, @xml2 AS xml2
    for xml path('')
)
select @xml3
于 2021-07-02T12:08:44.333 回答