14

属性の 1 つとして 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)

戻り値:

Property1   Property2
1           1
0           2
3           3

これは同じものを返します:

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)

前もって感謝します。

4

8 に答える 8

13

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

[not(@xsi:nil = "true")]

これにより、null が選択されます。ちなみに作者のコードにタイプミスがあります

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instace"

インスタンスのつづりが間違っています

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

作成者コードの作業バージョン

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)
于 2012-05-23T20:44:46.443 に答える
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) 
于 2011-01-28T21:53:51.453 に答える
5

フィールドを INT に設定しているため、xsi:nil="true" フィールドと値 0 の両方が INT Is 0 のデフォルト値として 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) 

この結果は次のようになります。

Property1   Property2
1           1
NULL        2
3           3
于 2010-07-09T07:03:30.463 に答える
2

私はこのアプローチを提案します:

DECLARE @a XML = '<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
        .value('./Property1[not(./@*[local-name()="nil"] = "true")][1]', 'int') as Property1,
    ParamValues.TaskChainerTask
        .value('./Property2[not(./@*[local-name()="nil"] = "true")][1]', 'int') as Property2
FROM @a.nodes('//Element') ParamValues(TaskChainerTask)
于 2013-05-20T18:39:16.807 に答える
2

必要に応じて NULLIF を使用して、空の文字列を NULL に変換しました。

nilを使用して生成できるFOR XMLようになりましたが、解析する方法がわかりませんでした...

于 2010-07-08T23:27:40.613 に答える
1

これを行う賢い方法は、null 値が必要な XML から Property1 ノードを削除することです。したがって、結果セットでnullにしたいノードは、XMLに追加しないでください。この方法では、xsi:nill 属性も追加する必要はありません。

したがって、以下もヌルになります。

declare @a xml
select @a = '<TestSet>
  <Element>
    <Property1>1</Property1>
    <Property2>1</Property2>
  </Element>
  <Element>
     //I have removed the property1 node and this will result in a null value
    <Property2>2</Property2>
  </Element>
  <Element>
    <Property1>3</Property1>
    <Property2>3</Property2>
  </Element>
</TestSet>'

 select ParamValues.TaskChainerTask.value('./Property1[1]','int') as Property1,
        ParamValues.TaskChainerTask.value('./Property2[1]','int') as Property2
   from @a.nodes('(/TestSet/Element)') as ParamValues(TaskChainerTask)

上記の例では、Property1 ノードがないため、null 値になることがわかります。

于 2014-11-07T10:25:51.500 に答える