在SQL Server中从XML中选择空值

asd*_*sdf 11 xml sql-server null xml-nil

我正在尝试从具有null作为其中一个属性的XML中进行选择.而不是返回null,它返回0.我做错了什么?
请参阅下面的代码进行复制:

declare @a xml
select @a = '<TestSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instace">
  <Element>
    <Property1>1</Property1>
    <Property2>1</Property2>
  </Element>
  <Element>
    <Property1 xsi:nil="true" />
    <Property2>2</Property2>
  </Element>
  <Element>
    <Property1>3</Property1>
    <Property2>3</Property2>
  </Element>
</TestSet>'

 select ParamValues.TaskChainerTask.query('Property1').value('.','int') as Property1,
        ParamValues.TaskChainerTask.query('Property2').value('.','int') as Property2
   from @a.nodes('(/TestSet/Element)') as ParamValues(TaskChainerTask)
Run Code Online (Sandbox Code Playgroud)

收益:

Property1   Property2
1           1
0           2
3           3
Run Code Online (Sandbox Code Playgroud)

这返回相同的东西:

declare @a xml
select @a = '<TestSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instace">
  <Element>
    <Property1>1</Property1>
    <Property2>1</Property2>
  </Element>
  <Element>
    <Property1 xsi:nil="true" />
    <Property2>2</Property2>
  </Element>
  <Element>
    <Property1>3</Property1>
    <Property2>3</Property2>
  </Element>
</TestSet>'

 select ParamValues.TaskChainerTask.query('Property1').value('.','int') as Property1,
        ParamValues.TaskChainerTask.query('Property2').value('.','int') as Property2
   from @a.nodes('(/TestSet/Element)') as ParamValues(TaskChainerTask)
Run Code Online (Sandbox Code Playgroud)

提前致谢.

pmb*_*tin 11

我想如果你使用number()函数,你会得到如预期的null.这仅适用于数字类型:

select 
   ParamValues.TaskChainerTask.query('Property1').value('number(.)','int') as Property1,         
   ParamValues.TaskChainerTask.query('Property2').value('number(.)','int') as Property2
from @a.nodes('(/TestSet/Element)') as ParamValues(TaskChainerTask) 
Run Code Online (Sandbox Code Playgroud)


Ese*_*sen 10

http://go4answers.webhost4life.com/Example/including-null-columns-empty-elements-125474.aspx

[not(@xsi:nil = "true")]
Run Code Online (Sandbox Code Playgroud)

这将选择null.顺便说一句,作者代码有一个错字

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instace"
Run Code Online (Sandbox Code Playgroud)

实例拼写为instace

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Run Code Online (Sandbox Code Playgroud)

作者代码的工作版本

declare @a xml
            select @a = '<TestSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
              <Element>
                <Property1>1</Property1>
                <Property2>1</Property2>
              </Element>
              <Element>
                <Property1 xsi:nil="true" />
                <Property2>2</Property2>
              </Element>
              <Element>
                <Property1>3</Property1>
                <Property2>3</Property2>
              </Element>
            </TestSet>'

             select ParamValues.TaskChainerTask.value('./Property1[1][not(@xsi:nil = "true")]','int') as Property1,
                    ParamValues.TaskChainerTask.value('./Property2[1][not(@xsi:nil = "true")]','int') as Property2
               from @a.nodes('(/TestSet/Element)') as ParamValues(TaskChainerTask)
Run Code Online (Sandbox Code Playgroud)


Jon*_*yne 5

因为您将字段设置为INT,所以您遇到的问题是xsi:nil ="true"字段和值0将最终为0,因为INT的默认值为0.

您可以首先转换为VARCHAR以检测包含xsi:nil ="true"的字符串字段生成的空字符串(''),然后将结果转换为INT.

这个SELECT将为您提供您所追求的答案

SELECT  CONVERT(INT,NULLIF(ParamValues.TaskChainerTask.query('Property1').value('.', 'varchar(5)'),'')) AS Property1
      , CONVERT(INT,NULLIF(ParamValues.TaskChainerTask.query('Property2').value('.', 'varchar(5)'),'')) AS Property2
FROM    @a.nodes('(/TestSet/Element)') AS ParamValues (TaskChainerTask) 
Run Code Online (Sandbox Code Playgroud)

结果给你:

Property1   Property2
1           1
NULL        2
3           3
Run Code Online (Sandbox Code Playgroud)


gbn*_*gbn 2

我只是根据需要使用 NULLIF 将空字符串转换为 NULL。

nil现在可以使用生成FOR XML,但我从未弄清楚如何解析它,抱歉......