Solved

Using QODBC

Posted on 2014-03-18
3
605 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
ID: 39939686
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
ID: 39939743
Thanks a lot; I meant INSERT (just standard SQL), that was a typo.

Rkorts
0
 
LVL 28

Expert Comment

by:Bill Bach
ID: 39939756
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Cassandra Select Query 1 87
Need a starter for ETL protocol? 4 65
compare between entities in entity framework 8 93
ServiceCenter IR Query Expressions 1 40
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

856 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