Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: SQL Syntax Help with MS Access DB in CR Post Reply Post New Topic
Author Message
MattK
Newbie
Newbie


Joined: 25 Jul 2013
Location: United States
Online Status: Offline
Posts: 3
Quote MattK Replybullet Topic: SQL Syntax Help with MS Access DB in CR
     Posted: 13 Jan 2015 at 6:20am
I'm trying to UNION ALL several tables from an MS Access DB into CR 2013 using SQL and I keep in running into errors. I don't have any issues with this syntax when coming from an Oracle DB (and I'm very much a newbie) so I'm assuming it's an Access thing (OLE DB ADO connection) where I'm just not knowledgeable on the correct syntax

Anyway, here's the sql and it would be great if somebody can give me some advice. Thanks!

SELECT *

FROM (SELECT invoice_header.invoice_date AS "date", tank_mix_chemicals.gallons_used AS "amount", 'Invoice' AS "type", brand.brand
FROM invoice_header
       LEFT OUTER JOIN invoice_tank_mix ON invoice_header.invoice_header_seq = invoice_tank_mix.invoice_header_seq
       LEFT OUTER JOIN tank_mix_chemicals ON tank_mix_chemicals.invoice_detail_seq = invoice_tank_mix.invoice_detail_seq
       LEFT OUTER JOIN brand ON brand.brand_seq = tank_mix_chemicals.brand_seq
       )
UNION ALL
SELECT *
FROM (SELECT transaction_header.transaction_date AS "date", transaction_detail.gallons AS "amount", transaction_type.transaction_type AS

"type", brand.brand
FROM transaction_header
LEFT OUTER JOIN transaction_detail ON transaction_header.transaction_header_seq = transaction_detail.transaction_header_seq
LEFT OUTER JOIN employee ON transaction_header.employee_seq = employee.employee_seq
LEFT OUTER JOIN agency ON transaction_header.agency_seq = agency.agency_seq
LEFT OUTER JOIN transaction_detail ON transaction_detail.transaction_type_seq = transaction_type.transaction_type_seq
)


Here's the error that I'm getting
[IMG]https://dl.dropboxusercontent.com/u/15099922/Presentation1.png" />

Edited by MattK - 13 Jan 2015 at 7:36am
IP IP Logged
kevlray
Admin Group
Admin Group
Avatar

Joined: 29 Oct 2009
Online Status: Offline
Posts: 1587
Quote kevlray Replybullet Posted: 13 Jan 2015 at 12:29pm
Not sure why you are doing a Select * then a sub-query.  But otherwise I believe the query is correct.  Just make sure your data types are the same for your joins on the tables.

Hope this helps.
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 16 Jan 2015 at 5:13am
I can't see the error, and Access is not my forte, does Access really use the "" around a fieldname, SQL Server would usually use []...

just a thought
IP IP Logged
MattK
Newbie
Newbie


Joined: 25 Jul 2013
Location: United States
Online Status: Offline
Posts: 3
Quote MattK Replybullet Posted: 16 Jan 2015 at 5:31am
Thanks for the responses. After much digging around yesterday I found the solution. Access wants the JOINS to be nested. For example, for the first query it would look like this

SELECT invoice_header.invoice_date AS [date], tank_mix_chemicals.gallons_used AS [amount], 'Invoice' AS [type], brand.brand

FROM ((invoice_header
    LEFT OUTER JOIN invoice_tank_mix ON invoice_header.invoice_header_seq = invoice_tank_mix.invoice_header_seq)
    LEFT OUTER JOIN tank_mix_chemicals ON tank_mix_chemicals.invoice_detail_seq = invoice_tank_mix.invoice_detail_seq)
    LEFT OUTER JOIN brand ON brand.brand_seq = tank_mix_chemicals.brand_seq


I figured it out by building the query in Access using it's query wizard and then looking at the SQL that it created. And as you can see I removed the sub-query (I had copied the syntax from an example that needed the sub-query) and added the brackets instead of the quotes.

Thanks again!


IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum