如何组合FOR XML PATH ROOT RAW ELEMENT?

时间:2015-04-21 09:58:48

标签: sql sql-server xml sqlxml

以下是我需要得到的输出:

    <myRoot xmlns="myNamespace" xmlns:xsi="http://www.xxx.zzz/XMLSchema-instance">
<myPath>
    <GrpHdr>
        <MsgId>decimal</MsgId>
    </GrpHdr>
    <Stmt>
        <Bal>
            <Tp>
                <Cd>string</Cd>
            </Tp>
        </Bal>
    </Stmt>
    <Amt Ccy="string">decimal</Amt>
</myPath>

问题(我的问题)是:

    <Amt Ccy="string">decimal</Amt> 

使用内部查询和FOR XML RAW我只能得到:

    <Amt Ccy="string" />

我仍然需要放置十进制值,这实际上是Amt值并用

关闭它
    </Amt>

可行吗?欢呼声。

    DECLARE @DayID datetime 
    DECLARE @Account numeric(26) 
    SELECT
    S.Statement         AS "GrpHdr/MsgId",
    'OBCD'              AS "Stmt/Bal/Tp/Cd",
    (SELECT [dbo].[GetAccountCurrency](@DayID , @Account )      AS "Ccy"
    FROM [output].[statements] AS S
    WHERE DayID = @DayID AND Account = @Account AND TYPE = 'D'
    FOR XML RAW ('Amt') , TYPE)
    FROM [output].[statements] AS S
    JOIN [dbo].[TAP00201]      AS T
    ON  DayID = date    AND 
    CAST (RIGHT(CONVERT(CHAR(26), Account ), 10) AS NUMERIC) = DMACCT
    WHERE DayID = @DayID AND Account = @Account AND TYPE = 'D'
    FOR XML PATH ('BkToCstmrStmtV01'), ROOT ('Document')

目前还没有任何价值,因为我没有任何最模糊的想法,如何用Amount值关闭它应该从记录表中获取或作为变量解析。

1 个答案:

答案 0 :(得分:0)

使用for xml path通常要容易得多:

...
select
    dbo.GetAccountCurrency(@DayID , @Account) as "@Ccy",
    'decimal' as "data()"
from [output].[statements] as S
where S.DayID = @DayID and S.Account = @Account and S.[Type] = 'D'
for xml path('Amt'), type
...