跳过从SQL生成的XML中的NULL参数

时间:2017-02-01 14:07:31

标签: sql-server xml

我想做什么?:从SQL Server 2014生成XML

问题:某些列包含NULL - 这些列的行为与XML中的属性相同。如果属性值为空/ null,则我的Validator不接受该属性。

示例

number      Type        Trat        Frecv 
----------- ----------- ---------- ----------- 
31301879    NULL          1           2           
73229903      2          NULL         2           
73229903      2           1           2  

期望的结果

<polita number="31301879" Trat="1" Frecv="2"></polita>
<polita number="73229903" Type="2" Frecv="2"></polita>
<polita number="73229903" Type="2" Trat="1" Frecv="2"></polita>

!!!免责声明:我已经在这里阅读了变量表的方法,但遗憾的是它对我不起作用,因为我的列需要是ATTRIBUTES而且我得到重复属性键

看起来像这样

declare @t table
(
 col1,
 col2
)
SELECT 
col1,
'' as col1,
col2,
'' as col2
FROM @t

这是我的代码

   WITH    Inreg
              AS ( SELECT DISTINCT
                            *
                   FROM     dbo.C1_UE C1
                            INNER JOIN dbo.C2_UE C2 ON C1.nrp = C2.ContractNumber
                   ORDER BY C1.nrp
                            OFFSET @Page * @PageSize ROWS FETCH NEXT @PageSize
                            ROWS ONLY
                 ),
            TotalP
              AS ( SELECT   COUNT(ContractNumber) AS Nr_pers
                   FROM     dbo.C2_UE
                 ),
            TotalS
              AS ( SELECT   REPLACE(ROUND(SUM(C1.Val_capital)
                                          + SUM(C3.Suma3), 0), '.00', '') AS total
                   FROM     dbo.C1_UE C1
                            LEFT JOIN dbo.C3_UE C3 ON C1.nrp = C3.ContractNumber
                 )
        SELECT  REPLACE(( SELECT    '' as 1
                                    ( SELECT    TotalP.Nr_pers
                                      FROM      TotalP
                                    ) AS '@nr_pers' ,
                                    ( SELECT    TotalS.total
                                      FROM      TotalS
                                    ) AS '@total' ,
                                    ( SELECT *
       --pers
                                                ( SELECT 
                                                         *
                                                  FROM    @C2 C
                                                          INNER JOIN @C1 CC ON CC.nrp = C.ContractNumber

                                                  WHERE   CC.nrp = C1.nrp
                                                FOR
                                                  XML PATH('pers') ,
                                                      TYPE
                                                ) ,
      --acc
                                                ( SELECT *
                                                  FROM    @C3 c
                                                          INNER JOIN @C1 CC ON CC.nrp = c.ContractNumber
                                                  WHERE   CC.nrp = C1.nrp
                                                FOR
                                                  XML PATH('even') ,
                                                      TYPE
                                                )
                                      FROM      @C1 C1 
                                    FOR
                                      XML PATH('poli') ,
                                          TYPE
                                    )
                        FOR
                          XML PATH('decl400')
                        ), '<decl400',
                        '<decl400 xmlns="mfp:aa:dd:d403:dec:v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="mfp:mfp:aa:dd:d403:dec:v1 file:/C:/Users/a/Desktop/dec.xsd" ');

@C1@C2@C3是上面声明的表格,@pageSize@pageNumber也是固定值。

2 个答案:

答案 0 :(得分:1)

你需要什么 - 如果我做对了 - 是标准行为:

DECLARE @Dummy TABLE(number INT,Type INT,Trat INT,Frecv INT);
INSERT INTO @Dummy VALUES
 (31301879,NULL,1,2)          
,(73229903,2,NULL,2)           
,(73229903,2,1,2);

SELECT number AS [@number]
      ,Type AS [@Type] 
      ,Trat AS [@Trat]
      ,Frecv AS [@Frecv]
FROM @Dummy 
FOR XML PATH('polita')

- 结果

<polita number="31301879" Trat="1" Frecv="2" />
<polita number="73229903" Type="2" Frecv="2" />
<polita number="73229903" Type="2" Trat="1" Frecv="2" />

如果您需要NULL而不是空字符串,则可以使用NULLIF()

This answer可以帮助您了解XML如何处理空与NULL

答案 1 :(得分:1)

Shnugo是我的第一个答案(+1),但如果你不想指定所有字段,另一个选择是使用XML RAW。例如

Declare @YourTable table (number int,[Type] int,Trat int,Frecv int)
Insert Into @YourTable values
 (31301879, NULL,1   , 2) 
,(73229903, 2   ,NULL, 2) 
,(73229903, 2   ,1   , 2) 

Select * from @YourTable for XML RAW('polita')

<强>返回

<polita number="31301879" Trat="1" Frecv="2" />
<polita number="73229903" Type="2" Frecv="2" />
<polita number="73229903" Type="2" Trat="1" Frecv="2" />
  

编辑 - 感谢Shnugo的RAW(&#39; polita&#39;)