2

プロパティ情報を返すAPIを使用しています。一部のテキスト情報は子ノード間で保存され、単一の文字列(VARCHAR)に連結したいと思います。

私のプロセスでは、Webサービスを介してxmlを取得し、これをprocに渡して、xml値を抽出し、ビューに挿入します。これは、返されるxmlのスニペットです。

<properties>
    <property propertyid="1234">
        <bullets>
            <bullet>nice garden</bullet>
            <bullet>it smells a bit</bullet>
            <bullet>body under the patio</bullet>
        </bullets>
    </property>
    ...
</properties>

これは、xmlがどのようにクエリされて値を抽出するかを垣間見ることができます。

INSERT        
INTO          VProperty
(             PropertyId,
              Description
)
SELECT        P.value('@propertyid', 'INT'),
              NULL -- extract all the bullet text values into a single string
FROM          @xml.nodes('/properties/property')

この例では、xmlから情報を抽出できるようにしたいので、最終的には次のようになります。

PropertyId    Description
1234          'nice garden\r\nit smells a bit\r\nbody under the patio

これは純粋なsql/xmlで可能になるのでしょうか、それともSQLランドに入る前にxmlで前処理を実行する必要があるのでしょうか。

(いつものように)どんな助けでも大歓迎です。

4

3 に答える 3

5

これはあなたのために働きますか?

DECLARE @XML XML = 
('<properties>
    <property propertyid="1234">
        <bullets>
            <bullet>nice garden</bullet>
            <bullet>it smells a bit</bullet>
            <bullet>body under the patio</bullet>
        </bullets>
    </property>
    <property propertyid="2345">
        <bullets>
            <bullet>bullet 2345_1</bullet>
            <bullet>bullet 2345_2</bullet>
            <bullet>bullet 2345_3</bullet>
        </bullets>
    </property>
</properties>');

SELECT  X.node.value('@propertyid', 'INT'),
        STUFF((SELECT '\r\n' + B.bullet.value('.', 'NVARCHAR(MAX)')
               FROM   X.node.nodes('./bullets/bullet') B ( bullet )
               FOR XML PATH(''),TYPE).value('.', 'NVARCHAR(MAX)'),
              1, 4, '') AS Description
FROM    @xml.nodes('/properties/property') X ( node );
于 2012-10-21T19:16:09.727 に答える
0

このXQueryを使用できます(SQL Server 2008のXQueryの方言が完全に互換性があるかどうかはわかりません)。

for $pr in /*/property,
    $id in $pr/@propertyid,
    $desc in string-join($pr/*/*, '&#xD;&#xA;')
  return
     (<PropertyId>{string($id)}</PropertyId>,
     <Description>{$desc}</Description>)

SaxonとXQSharp(XmlPrime)を使用すると、提供されたXMLドキュメントにこのクエリを適用した結果は次のようになります

<properties>
    <property propertyid="1234">
        <bullets>
            <bullet>nice garden</bullet>
            <bullet>it smells a bit</bullet>
            <bullet>body under the patio</bullet>
        </bullets>
    </property>
    ...
</properties>

です

<PropertyId>1234</PropertyId>
<Description>nice garden&#xD;
it smells a bit&#xD;
body under the patio</Description>
于 2012-10-21T19:27:27.537 に答える
0
DECLARE @XML XML
SET @XML = 
'<properties>
  <property propertyid="1234">
    <bullets>
      <bullet>nice garden</bullet>
      <bullet>it smells a bit</bullet>
      <bullet>body under the patio</bullet>
    </bullets>
  </property>
  <property propertyid="2345">
    <bullets>
      <bullet>bullet 2345_1</bullet>
      <bullet>bullet 2345_2</bullet>
      <bullet>bullet 2345_3</bullet>
    </bullets>
  </property>
</properties>'

SET @XML = @XML.query('
    for $prop in /properties/property
    return
    <property propertyid="{string($prop/@propertyid)}">
        <description>
        {
            for $bullet in $prop/bullets[1]/bullet
            return
                if ($bullet is $prop/bullets[1]/bullet[1] and $bullet << $prop/bullets[1]/bullet[last()])
                then concat(string($bullet), "\r\n")
                else
                    if ($bullet is $prop/bullets[1]/bullet[last()])
                    then string($bullet)
                    else concat(string($bullet), "\r\n")
        }    
        </description>
    </property>
')

SELECT
Tab.ColXML.value('@propertyid', 'INTEGER') AS PropertyId,
Tab.ColXML.value('description[1]', 'NVARCHAR(128)') AS Description
FROM @XML.nodes('//property') AS Tab(ColXML)

MS SQL Server 2005にはlet句がないため、回避策を見つける必要があります。

query()メソッドの式では、2つのネストされた「ループのような」句を使用して、必要な要素を選択します。[property]要素を構築する最初の[for句]。[bullet]要素のリストから[property]の子要素である[description]要素を構築する2番目の[for句]。

[description]は、次のように[bullet]要素のリストから構築されたシーケンスから値を取得します。

最後ではなく最初の[箇条書き]に「\r\n」が追加されます。

最初ではなく最後ではない[箇条書き]にも「\r\n」が追加されます。

他の[弾丸]はそのままにしておきます。

それぞれに作成されたすべての文字列を連結して、関連する[description]の値を作成します。

于 2016-09-16T04:55:05.633 に答える