2016-09-09 21 views
1

のノードを検出する私は、以下のようなXMLファイルから読み込み、その値をフォームの関連フィールドにインポートするSQLトリガーを持っています。SQLを使用してフィールド値が

<?xml version="1.0" encoding="UTF-8"?> 
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2016-08-18T07:03:22"> 
    <MPSExport> 
     <ReportID>WR_2245</ReportID> 
     <ActivityID>0</ActivityID> 
     <Hours>13.75</Hours> 
    </MPSExport> 
    <MPSExport> 
     <ReportID>WR_2245</ReportID> 
     <ActivityID>115810</ActivityID> 
     <Method>5</Method> 
     <Herbicide_1>5</Herbicide_1> 
     <Herbicide_Rate_1>0.05</Herbicide_Rate_1> 
     <Herbicide_Qty_1>20</Herbicide_Qty_1> 
     <Herbicide_2>2</Herbicide_2> 
     <Herbicide_Rate_2>0.5</Herbicide_Rate_2> 
     <Herbicide_Qty_2>60</Herbicide_Qty_2> 
     <Herbicide_IsSurfactant_2>1</Herbicide_IsSurfactant_2> 
     <Comments>Test.</Comments> 
    </MPSExport> 
    <MPSExport> 
     <ReportID>WR_2245</ReportID> 
     <ActivityID>115810</ActivityID> 
     <Method>8</Method> 
     <Herbicide_1>10</Herbicide_1> 
     <Herbicide_Rate_1>2</Herbicide_Rate_1> 
     <Herbicide_Qty_1>30</Herbicide_Qty_1> 
     <Herbicide_2>2</Herbicide_2> 
     <Herbicide_Rate_2>1</Herbicide_Rate_2> 
     <Herbicide_Qty_2>70</Herbicide_Qty_2> 
     <Herbicide_IsSurfactant_2>1</Herbicide_IsSurfactant_2> 
     <Weed_1>5266</Weed_1> 
     <Weed_2>5261</Weed_2> 
     <Weed_3>5884</Weed_3> 
     <Weed_4>4004</Weed_4> 
     <Comments>WR_2245 finished off some of WMA_620&apos;s budget (MPS BLM Bal Est BC). Began treating rambling dock and winter cherry.</Comments> 
     <FollowUpNotes>Continue to work through the Zone</FollowUpNotes> 
    </MPSExport> 
</dataroot> 

同じ名前で繰り返されるフィールドがあるため、フォーム上の異なるタブのフィールドに対応します。誰かがXMLファイルに空白「メソッド」フィールドの最初の発生を離れるが、第2の発生が値を持っている場合は、これらを区別するために、私は、

SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 100)) AS ROW, 'Method', CAST([Method]AS VARCHAR(2)) FROM OPENXML(@hDoc, 'dataroot/MPSExport') WITH ([Method] [nvarchar](max) 'Method') WHERE [Method] IS NOT NULL 
     UNION ALL.....continue with other fields 

問題があるを使用し、私のインポートプロセスは、最初のメソッドフィールドに移入されますXMLファイル内の2番目のMethod値を持つフォーム上に配置します。

フィールド名を繰り返してグループ化するのではなく、親ノードがXMLフィールドをグループ化する方法がありますか?つまり、MPSExport?

は、一つのフィールド上のテストとして、私は

SELECT 'Method', CAST([Method]AS VARCHAR(2)), 
ROW_NUMBER() OVER (ORDER BY T.X) AS ROW 
FROM @XML.nodes('dataroot/MPSExport') AS T(X) 
CROSS APPLY 
OPENXML(@hDoc, 'dataroot/MPSExport') WITH ([Method] [nvarchar](max) 'Method') 
WHERE [Method] IS NOT NULL 

を試してみましたが、私はそれが

Method 5 2 
Method 8 3 

5と8ですが見せたいのに対し、それはちょうど私に次

Method 5 1 
Method 8 2 
Method 5 3 
Method 8 4 
Method 5 5 
Method 8 6 

を与えます値、2と3はMPSExportノードのオカレンスです。 これは可能ですか?

答えて

0

まず、FROM OPENXMLは古くなっており、もう使用しないでください。

ベターはCROSS APPLYとコードが各-WITH-各ジョイン、したがって複数行の結果を生成.value().nodes().modify().query()

よう最新 XMLメソッドを使用します。

問題は番号すべてのメソッドですが、値を持つ行だけを表示することです。

私のアプローチ:を使用して<MSPExport>すべての要素を取得し、<Method>があるかどうかに番号を付けてください。次に、この派生テーブルを取って、nullでないメソッドをフィルタリングします。

この1 <MSPExport>下記つ以上の<Method>がある可能性があれば、問題がある可能性があります...

DECLARE @xml XML= 
N'<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2016-08-18T07:03:22"> 
    <MPSExport> 
     <ReportID>WR_2245</ReportID> 
     <ActivityID>0</ActivityID> 
     <Hours>13.75</Hours> 
    </MPSExport> 
    <MPSExport> 
     <ReportID>WR_2245</ReportID> 
     <ActivityID>115810</ActivityID> 
     <Method>5</Method> 
     <Herbicide_1>5</Herbicide_1> 
     <Herbicide_Rate_1>0.05</Herbicide_Rate_1> 
     <Herbicide_Qty_1>20</Herbicide_Qty_1> 
     <Herbicide_2>2</Herbicide_2> 
     <Herbicide_Rate_2>0.5</Herbicide_Rate_2> 
     <Herbicide_Qty_2>60</Herbicide_Qty_2> 
     <Herbicide_IsSurfactant_2>1</Herbicide_IsSurfactant_2> 
     <Comments>Test.</Comments> 
    </MPSExport> 
    <MPSExport> 
     <ReportID>WR_2245</ReportID> 
     <ActivityID>115810</ActivityID> 
     <Method>8</Method> 
     <Herbicide_1>10</Herbicide_1> 
     <Herbicide_Rate_1>2</Herbicide_Rate_1> 
     <Herbicide_Qty_1>30</Herbicide_Qty_1> 
     <Herbicide_2>2</Herbicide_2> 
     <Herbicide_Rate_2>1</Herbicide_Rate_2> 
     <Herbicide_Qty_2>70</Herbicide_Qty_2> 
     <Herbicide_IsSurfactant_2>1</Herbicide_IsSurfactant_2> 
     <Weed_1>5266</Weed_1> 
     <Weed_2>5261</Weed_2> 
     <Weed_3>5884</Weed_3> 
     <Weed_4>4004</Weed_4> 
     <Comments>WR_2245 finished off some of WMA_620&apos;s budget (MPS BLM Bal Est BC). Began treating rambling dock and winter cherry.</Comments> 
     <FollowUpNotes>Continue to work through the Zone</FollowUpNotes> 
    </MPSExport> 
</dataroot>'; 

クエリ

WITH NumberdMethods AS 
(
    SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS [ROW] 
      ,ex.value('Method[1]','int') AS Method 
    FROM @xml.nodes('/dataroot/MPSExport') AS A(ex) 
) 
SELECT * 
FROM NumberdMethods 
WHERE Method IS NOT NULL 

結果

ROW Method 
2 5 
3 8 
が--Hereです
+0

お返事ありがとうございました。私はトリガーを更新します。非常に役立ちます。 – user3882865

関連する問題