JDBC Driver for QuickBooks

Build 23.0.8843

親/子レコードの挿入

ユースケース

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

例えば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 QuickBooks

For a working example of how temp tables can be used for bulk insert in QuickBooks, please see the following:

// Insert into Invoices table
INSERT INTO InvoiceLineItems#TEMP (ItemName, ItemQuantity) VALUES ('Repairs','1')
INSERT INTO InvoiceLineItems#TEMP (ItemName, ItemQuantity) VALUES ('Removal','2')

INSERT INTO Invoices (CustomerName, Memo, ItemAggregate) VALUES ('Abercrombie, Kristy', 'NUnit Memo', 'InvoiceLineItems#TEMP')


// Insert into InvoiceLineItems table
INSERT INTO InvoiceLineItems#TEMP (CustomerName, Date, ShipMethod, ShipDate, Memo, Message, DueDate, Other, ItemName, ItemQuantity, ItemRate) VALUES ('Abercrombie, Kristy', '2011-01-01', 'UPS', '2011-01-02', 'NUnit Memo', 'We appreciate your prompt payment.', '2011-01-03', 'Some other data', 'Repairs', '1', '3.50')
INSERT INTO InvoiceLineItems#TEMP (CustomerName, Date, ShipMethod, ShipDate, Memo, Message, DueDate, Other, ItemName, ItemQuantity, ItemRate) VALUES ('Abercrombie, Kristy', '2011-01-01', 'UPS', '2011-01-02', 'NUnit Memo', 'We appreciate your prompt payment.', '2011-01-03', 'Some other data', 'Removal', '2', '3.50')

INSERT INTO InvoiceLineItems (CustomerName, Date, ShipMethod, ShipDate, Memo, Message, DueDate, Other, ItemName, ItemQuantity, ItemRate) SELECT CustomerName, Date, ShipMethod, ShipDate, Memo, Message, DueDate, Other, ItemName, ItemQuantity, ItemRate InvoiceLineItems#TEMP


//Insert into JournalEntry
INSERT Into JournalEntries#TEMP (ReferenceNumber, LineAggregate) VALUES   ('Setup 13', '<JournalEntryLines> <Row><LineType>Credit</LineType><LineAccount>Retained Earnings</LineAccount><LineAmount>100</LineAmount></Row> <Row><LineType>Credit</LineType><LineAccount>Note Payable - Bank of Anycity</LineAccount><LineAmount>20</LineAmount></Row> <Row><LineType>Debit</LineType><LineAccount>Checking</LineAccount><LineAmount>120</LineAmount></Row> </JournalEntryLines>');
INSERT Into JournalEntries#TEMP (ReferenceNumber, LineAggregate) VALUES   ('Setup 14','<JournalEntryLines> <Row><LineType>Credit</LineType><LineAccount>Retained Earnings</LineAccount><LineAmount>100</LineAmount></Row> <Row><LineType>Credit</LineType><LineAccount>Note Payable - Bank of Anycity</LineAccount><LineAmount>20</LineAmount></Row> <Row><LineType>Debit</LineType><LineAccount>Checking</LineAccount><LineAmount>120</LineAmount></Row> </JournalEntryLines>' );
INSERT INTO JournalEntries (ReferenceNumber,LineAggregate) SELECT ReferenceNumber,LineAggregate from JournalEntries#TEMP;



//Insert into TimeTracking table
INSERT INTO timetracking#TEMP (BillableStatus, Duration, Date, EmployeeId,CustomerId,CustomerName, ServiceItemName, ServiceItemId) Values ('Billable','4:10','2002-09-09',  '370000-933272659', '800013DE-1702639725', 'Testing02','Blueprint changess','340000-1071522351');
INSERT INTO timetracking#TEMP (BillableStatus, Duration, Date, EmployeeId,CustomerId,CustomerName, ServiceItemName, ServiceItemId) Values ('Empty','1:10','2002-09-02',  '370000-933272659', '800013DE-1702639725', 'Testing02','Blueprint changess','340000-1071522351');
INSERT INTO timetracking (BillableStatus, Duration, Date, EmployeeId,CustomerId,CustomerName, ServiceItemName, ServiceItemId) SELECT BillableStatus, Duration, Date, EmployeeId,CustomerId,CustomerName, ServiceItemName, ServiceItemId from timetracking#TEMP;

//Insert into SalesReceipts
INSERT INTO SalesReceiptLineItems#TEMP (CustomerName, ItemName, ItemQuantity) VALUES ('Theodor', 'Repairs', 1);
INSERT INTO SalesReceipts#TEMP (CustomerName, ItemAggregate) VALUES ('name1', SalesReceiptLineItems#TEMP);
INSERT INTO SalesReceipts#TEMP (CustomerName, ItemAggregate) VALUES ('name2',SalesReceiptLineItems#TEMP);
INSERT INTO SalesReceipts (CustomerName, ItemAggregate) SELECT CustomerName, ItemAggregate from SalesReceipts#TEMP;

//Insert into Bills
INSERT INTO BillLineItems#TEMP (ReferenceNumber, ItemName, ItemQuantity) VALUES (1, 'Repairs','1');
INSERT INTO BillLineItems#TEMP (ReferenceNumber, ItemName, ItemQuantity) VALUES (2, 'Removal','2');
INSERT INTO Bills#TEMP (ReferenceNumber, VendorName, Date, ItemAggregate) VALUES (1, 'Cal Telephone', '1/1/2021', 'BillLineItems#TEMP');
INSERT INTO Bills#TEMP (ReferenceNumber, VendorName, Date, ItemAggregate) VALUES (2, 'Cal Telephone', '1/2/2021', 'BillLineItems#TEMP');
INSERT INTO Bills (ReferenceNumber, VendorName, Date, ItemAggregate) select ReferenceNumber, VendorName, Date, ItemAggregate from Bills#TEMP");

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