将属性添加到由FOR XML PATH生成的根元素

时间:2018-11-26 17:49:08

标签: sql sql-server xml

我环顾四周,发现了一些样本,但目前对我来说没有多大意义。我试图将属性添加到根。我确实看到的一个样本建议使用临时表,这种表虽然很合理,但仍然有些混乱。

这是我当前的查询:

    DECLARE
@inv_code nvarchar(100)
--as
if (isnull(@inv_code,'')<>'' or ISNUMERIC(@inv_code)=1)
begin
    SELECT  
        net_inv_code,--11/21/2018 MAH 
        net_ivd_key=ivd_key, 
        net_ivd_price=convert(decimal(9,2),ivd_price), 
        net_ivd_qty=convert(decimal(9,2),ivd_qty), 
        net_ivd_amount_cp=convert(decimal(9,2),linetotal), 
        net_prc_display_name=prd_name, 
        ivd_order=CASE WHEN ivd_prc_prd_ptp_key = '1E30F230-3750-4747-8ACC-36F50B5CC610' THEN '100000' ELSE lineNumber END,  
        net_tracking_number=trackingNumber, 
        net_prc_code=prc_code, 
        net_linebalance, 
        net_fws_ptp_type=ivd_prc_prd_ptp_key, 
        [status], ivd_prc_prd_key 
    FROM vw_client_aha_ac_invoice_detail_integrated  
    WHERE net_inv_code=@inv_code
    ORDER BY ivd_order
    for xml path('ivd'), root('inv net_inv_code=' + @inv_code)
end
else
begin
    select 
        null as net_inv_code,--11/21/2018 MAH
        null as ivd_key,
        null as net_ivd_price,
        null as net_ivd_qty,
        null as net_ivd_amount_cp,
        null as net_prc_display_name,
        null as ivd_order,
        null as net_tracking_number,
        null as net_prc_code,
        null as net_linebalance,
        null as net_fws_ptp_type,
        null as [status],
        null as ivd_prc_prd_key
        for xml path('ivd'), root('inv')
end

我尝试调整此行:

for xml path('ivd'), root('inv net_inv_code=' + @inv_code)

我认为您无法像我希望的那样编辑该路径。这就是我试图返回的东西:

<inv net_inv_code="1001538">
<ivd>
    <net_inv_code>1001538</net_inv_code>
    <net_ivd_key>74A440ED-8CF6-4D57-8362-6EBC97CB8221</net_ivd_key>
    <net_ivd_price>0.00</net_ivd_price>
    <net_ivd_qty>0.00</net_ivd_qty>
    <net_ivd_amount_cp>0.00</net_ivd_amount_cp>
    <net_prc_display_name>SHSMD U 2018 | Healthcare Marketing Credential: Marketing Plans, Communications &amp; Market Research</net_prc_display_name>
    <net_prc_code>309HMC2018-M</net_prc_code>
    <net_linebalance>0.0000</net_linebalance>
    <net_fws_ptp_type>64B77FB4-DA34-4034-ADA8-F1DA796D8720</net_fws_ptp_type>
    <ivd_prc_prd_key>EE4DFB27-3171-4463-B300-1DDCBB71D877</ivd_prc_prd_key>
</ivd>
<ivd>
    <net_inv_code>1001538</net_inv_code>
    <net_ivd_price>0.00</net_ivd_price>
    <net_ivd_qty>0.00</net_ivd_qty>
    <net_ivd_amount_cp>0.00</net_ivd_amount_cp>
    <ivd_order>99999</ivd_order>
    <net_prc_code>Tax</net_prc_code>
    <net_linebalance>0.0000</net_linebalance>
    <net_fws_ptp_type>F53B02CB-7623-49B4-A59A-55B0547F291C</net_fws_ptp_type>
</ivd>

当前,我遇到语法错误。寻找最佳/最简单的解决方案

更新: 根据您的建议,这是我尝试过的但遇到语法错误:

    SELECT @inv_code AS '@net_inv_code', 
(
    SELECT  
        net_inv_code,--11/21/2018 MAH 
        net_ivd_key=ivd_key, 
        net_ivd_price=convert(decimal(9,2),ivd_price), 
        net_ivd_qty=convert(decimal(9,2),ivd_qty), 
        net_ivd_amount_cp=convert(decimal(9,2),linetotal), 
        net_prc_display_name=prd_name, 
        ivd_order=CASE WHEN ivd_prc_prd_ptp_key = '1E30F230-3750-4747-8ACC-36F50B5CC610' THEN '100000' ELSE lineNumber END,  
        net_tracking_number=trackingNumber, 
        net_prc_code=prc_code, 
        net_linebalance, 
        net_fws_ptp_type=ivd_prc_prd_ptp_key, 
        [status], ivd_prc_prd_key 
    FROM vw_client_aha_ac_invoice_detail_integrated  
    WHERE net_inv_code=@inv_code
    ORDER BY ivd_order
) AS v
FOR XML PATH('ivd'), TYPE
)
FOR XML PATH('inv')

1 个答案:

答案 0 :(得分:1)

以下查询:

DECLARE @inv_code VARCHAR(100) = '1001538';

SELECT TRY_CAST(@inv_code AS INT) AS '@net_inv_code', (
    SELECT *
    FROM (VALUES
        (1001538, 'x'),
        (1001538, 'x')
    ) AS v(net_inv_code, net_ivd_key)
    FOR XML PATH('ivd'), TYPE
)
FOR XML PATH('inv')

Produces the following result

<inv net_inv_code="1001538">
    <ivd>
        <net_inv_code>1001538</net_inv_code>
        <net_ivd_key>x</net_ivd_key>
    </ivd>
    <ivd>
        <net_inv_code>1001538</net_inv_code>
        <net_ivd_key>x</net_ivd_key>
    </ivd>
</inv>

这似乎是您想要的。如果net_inv_code不是整数,将添加@inv_code属性。