Solved

Using QODBC

Posted on 2014-03-18
3
594 Views
Last Modified: 2014-03-19
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
Comment
Question by:Richard Korts
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
Bill Bach earned 500 total points
Comment Utility
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
 

Author Comment

by:Richard Korts
Comment Utility
Thanks a lot; I meant INSERT (just standard SQL), that was a typo.

Rkorts
0
 
LVL 28

Expert Comment

by:Bill Bach
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now