Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 635
  • Last Modified:

Using QODBC

I want to use QODBC; I have a couple of (I think) fairly simple requirements.

(1) Using a csv file imported from elsewhere, either create a new customer record or update an existing one; one row in csv file per customer.

(2) Export a large portion of (specific fields) of the customer table to a csv file.

(3) Ditto to (2) for Invoice & Invoiceline.

Can someone point me to examples that are similar or close to this?

Thanks
0
Richard Korts
Asked:
Richard Korts
  • 2
1 Solution
 
Bill BachPresidentCommented:
1) I do not believe that QODBC has an UPSERT command.  This means that you will need to build some logic into your environment to detect whether a customer already exists, then decide to use either an INSERT or an UPDATE statement, accordingly.  This is not too difficult with a VBScript program that reads a line from your CSV file, checks if the record exists first, then branches as needed.  This could look like this:
	ExistingCust = "SELECT TOP 1 ListID FROM Customer WHERE Name = '" & NameFromCSV & "'
	Set FoundName = conn.Execute(ExistingCust)
	if not FoundName.EOF then

Open in new window


Once you branch, the INSERT and UPDATE statements are standard SQL, and the examples from the QODBC documentation should suffice.  

2) Exporting data is easy.  A simple SQL Statement like "SELECT * FROM Customer" will give you all of the fields.  If you want to export it to CSV, then you can use a GUI tool like MSAccess or MSExcel to read and export the data, or you can use a command line tool like SQLExec like this:  SQLEXEC "QuickBooksData" "Select * from Customer Order By Name" /oc

In my own use of QODBC, I use this statement to export my customer data:

SELECT Customer.FullName, Customer.Contact, Customer.Phone, Customer.EMail, Customer.BillAddressCity, Customer.BillAddressState, Customer.BillAddressAddr1, Customer.BillAddressAddr2, Customer.BillAddressAddr3, Customer.BillAddressAddr4, Customer.BillAddressPostalCode, Customer.BillAddressCountry
FROM Customer NOSYNC
WHERE (Customer.CustomerTypeRefFullName<>'CLOSED')
ORDER BY Customer.CompanyName

Obviously, you can add fields that you need or remove those that you do not.

3) Exports for these are the same -- simple select statements via any ODBC-compatible tool.  Again, I do these nightly via an automated SQLExec script that dumps my data into a second database which allows for faster searches, and I use a query like this:

SELECT 
	Invoice.RefNumber,
	Invoice.TxnDate,
	Invoice.PONumber,
	Invoice.CustomerRefFullName,
	Invoice.SubTotal,
	Invoice.BalanceRemaining,
	Invoice.BillAddressAddr1,
	Invoice.BillAddressAddr2,
	Invoice.BillAddressAddr3,
	Invoice.BillAddressAddr4,
	Invoice.BillAddressCity,
	Invoice.BillAddressState,
	Invoice.BillAddressPostalCode,
	Invoice.BillAddressCountry,
	Invoice.ShipAddressAddr1,
	Invoice.ShipAddressAddr2,
	Invoice.ShipAddressAddr3,
	Invoice.ShipAddressAddr4,
	Invoice.ShipAddressCity,
	Invoice.ShipAddressState,
	Invoice.ShipAddressPostalCode,
	Invoice.ShipAddressCountry,
	Invoice.SalesTaxTotal
FROM 
        Invoice NOSYNC
WHERE
	{fn TIMESTAMPDIFF(SQL_TSI_DAY, Invoice.TxnDate, {fn CURDATE()})} < 30
ORDER BY 
	Invoice.RefNumber;

SELECT 
	InvoiceLine.RefNumber,
	InvoiceLine.InvoiceLineSeqNo,
	InvoiceLine.InvoiceLineQuantity,
	InvoiceLine.InvoiceLineItemRefFullName,
	InvoiceLine.InvoiceLineDesc,
	InvoiceLine.InvoiceLineRate,
	InvoiceLine.InvoiceLineAmount
FROM 
        InvoiceLine NOSYNC
WHERE 
	{fn TIMESTAMPDIFF(SQL_TSI_DAY, InvoiceLine.TxnDate, {fn CURDATE()})} < 30
ORDER BY 
	InvoiceLine.RefNumber;

Open in new window


Note that these queries grab the last 30 days of invoices, because I assume that we are not changing anything older than 30 days back.  Also, notice the use of NOSYNC because I force a synchronization of the QODBC cache immediately before my scripts run by calling "sp_optimizeupdatesync Invoice" first.
0
 
Richard KortsAuthor Commented:
Thanks a lot; I meant INSERT (just standard SQL), that was a typo.

Rkorts
0
 
Bill BachPresidentCommented:
Actually, "UPSERT" is a command in some environments, especially ETL tools.  It means "UPDATE the record if it exists, or INSERT the record if it does not exist".  It greatly simplifies the work of loading data from an import file.  Without this feature, you have to do the heavy lifting yourself in code.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now