Power BI Connector for Azure DevOps

Build 23.0.8839

親/子レコードの挿入

ユースケース

レコードを挿入する際、親レコードに依存する子レコードに関する詳細を入力する必要がある状況は度々発生します。

例えばCRM システムを使う場合には、Invoices を入力するには最低1つの明細行が必要です。請求書の行項目は複数のフィールドを持つことができるため、こうしたInvoices の仕様は、データをリレーショナルテーブルとして提供する際に問題となります。

データを読み込む際、外部キーで接続されたInvoice とInvoiceLineItem テーブルをモデル化するのは容易です。しかし挿入の際には、CRM システムはInvoice およびInvoiceLineItems を1つの操作で作成する必要があります。

こうした問題を解決するために、CData のツールは親コレクションカラム上に子コレクションカラムを提供します。これらのカラムは、親および子レコード両方の情報を持つINSERT ステートメントを発行する際に使用することができます。

例えば、Invoice テーブルにInvoiceLineItems というシングルカラムがあるとします。 挿入の際には、InvoiceLineItems テーブルに挿入する必要があるレコードの詳細を、Invoice レコードのInvoiceLineItems カラムに渡すことができます。

次のセクションでは、この方法を説明します。

親 / 子レコードを挿入する方法

親 / 子レコードを挿入するために、本製品 は2つの方法を用意しています。一時テーブルを使った挿入と、XML への集約による挿入です。

一時(#TEMP)テーブル

データを入力する最も簡易な方法は、本製品 がメモリに格納する#TEMP テーブル、または一時テーブルを使用することです。

#TEMP テーブルは以下の構文で参照してください。

TableName#TEMP

#TEMP テーブルは接続中はメモリに格納されます。

そのため、これらのテーブルを使用するには挿入の発行間で接続を閉じることはできず、各クエリで異なる接続を使用する環境では一時テーブルは使用できません。

単一の接続内では、テーブルはバルク挿入が成功するまでメモリに残り、成功した時点でメモリから消去されます。

例:

INSERT INTO InvoiceLineItems#TEMP (ReferenceNumber, Item, Quantity, Amount) VALUES ('INV001', 'Basketball', 10, 9.99)
INSERT INTO InvoiceLineItems#TEMP (ReferenceNumber, Item, Quantity, Amount) VALUES ('INV001', 'Football', 5, 12.99)

InvoiceLineItems テーブルに書き込みが行われた後で、Invoice テーブルへの挿入中に#TEMP テーブルが参照されます。

INSERT INTO Invoices (ReferenceNumber, Customer, InvoiceLines) VALUES ('INV001', 'John Doe', 'InvoiceLineItems#TEMP')

内部では本製品 が#TEMP テーブルから値を読み込みます。

ReferenceNumber は、どのInvoice に明細が紐付けられているかを特定するのに使用されています。これは、各Invoice に別々の明細が記載されている状況で、#TEMP テーブルにバルク挿入で書き込みが行われ、使用される可能性があるためです。 これで#TEMP テーブルをバルク挿入で使用することができます。例:

INSERT INTO Invoices#TEMP (ReferenceNumber, Customer, InvoiceLines) VALUES ('INV001', 'John Doe', 'InvoiceLineItems#TEMP')
INSERT INTO Invoices#TEMP (ReferenceNumber, Customer, InvoiceLines) VALUES ('INV002', 'Jane Doe', 'InvoiceLineItems#TEMP')
INSERT INTO Invoices SELECT ReferenceNumber, Customer, InvoiceLines FROM Invoices#TEMP

この場合、2つの異なるInvoice を挿入しています。ReferenceNumber によって、どのLines がどのInvoice に入力されるかを決定します。

Note:ここで示したテーブルとカラムは、本製品 が一般にどのように動作するかを示す一例です。特定のテーブルおよびカラム名は、本製品 内で異なることがあります。

XMLの直接挿入

#TEMP テーブルの代替として、XML を直接使うこともできます。#TEMP テーブルはXML を作成するためには使われないので、同じ接続を使うか挿入後に接続を閉じるかは影響しません。

例:

[
  {
    "Item", "Basketball",
    "Quantity": 10
    "Amount": 9.99
  },
  {
    "Item", "Football",
    "Quantity": 5
    "Amount": 12.99
  }
]

または

<Row>
  <Item>Basketball</Item>
  <Quantity>10</Quantity>
  <Amount>9.99</Amount>
</Row>
<Row>
  <Item>Football</Item>
  <Quantity>5</Quantity>
  <Amount>12.99</Amount>
</Row>

XML はその性質上、親レコードに対して挿入ごとに全体で渡されるため、これらの例ではReferenceNumber が存在しないことに注意してください。 行ごとに完全なXML を作成および発行する必要があるので、親レコードに子レコードを紐付け直すものは必要ありません。

次に、値を挿入します。

INSERT INTO Invoices (ReferenceNumber, Customer, InvoiceLines) VALUES ('INV001', 'John Doe', '{...}')

または

INSERT INTO Invoices (ReferenceNumber, Customer, InvoiceLines) VALUES ('INV001', 'John Doe', '<Row>...</Row>')

Note:本製品 はXML / JSON 集約の使用もサポートします。

Example for Azure DevOps

For a working example of how temp tables can be used to insert data in Azure DevOps, please see the following. In Azure DevOps,

Note: the key references such as Id may be different in your environment:

// Execute bulk insert

INSERT INTO TestResults#TEMP (Projectid, Testrunid, TestCaseTitle, AutomatedTestName, Priority, Outcome) VALUES ('b154d8f3-bfd9-4bfb-90ae-2e6c8cda8937',1, 'VerifyWebsiteTheme', 'FabrikamFiber.WebSite.TestClass.VerifyWebsiteTheme', 1, 'Passed');

INSERT INTO TestResults#TEMP (Projectid, Testrunid, TestCaseTitle, AutomatedTestName, Priority, Outcome) VALUES ('b154d8f3-bfd9-4bfb-90ae-2e6c8cda8937',1, 'VerifyWebsiteLinks', 'FabrikamFiber.WebSite.TestClass.VerifyWebsiteLinks', 2, 'Failed');

INSERT INTO TestResults (Projectid, Testrunid, TestCaseTitle, AutomatedTestName, Priority, Outcome) SELECT Projectid, Testrunid, TestCaseTitle, AutomatedTestName, Priority, Outcome FROM testresults#TEMP

// Execute bulk update

INSERT INTO TestResults#TEMP (Projectid, Testrunid, Id, Comment, State) VALUES ('b154d8f3-bfd9-4bfb-90ae-2e6c8cda8937',1, 10000, 'Website theme is looking good', 'Completed');

INSERT INTO TestResults#TEMP (Projectid, Testrunid, Id, Comment, State) VALUES ('b154d8f3-bfd9-4bfb-90ae-2e6c8cda8937',1, 100001, 'Website links are failing because of incorrect container id', 'Completed');

Update TestResults (Projectid, Testrunid, Id, Comment, State) SELECT Projectid, Testrunid, Id, Comment, State FROM testresults#TEMP

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 23.0.8839