Multiple Union on Local Excel Linked Tables (1 Viewer)

Kheribus

Registered User.
Local time
Today, 15:53
Joined
Mar 30, 2015
Messages
97
I have the following query to union three separate Excel File local linked tables into one query result.

Code:
SELECT regtrips.[Name] As tripName, regtrips.[Schools] As schools, regtrips.[Number Stops] As numStops, regtrips.[Number Assigned] As numAssigned, regtrips.Duration as [duration], regtrips.[Start Time] As startTime, regtrips.[Finish Time] As endTime, regtrips.[Distance] As distance, regtrips.[Bus Type] As busType, regtrips.[Dates] As dateInfo, regtrips.[Description] As description FROM regtrips
UNION SELECT spedtrips.[Name], spedtrips.[Schools], spedtrips.[Number Stops], spedtrips.[Number Assigned], spedtrips.[Duration], spedtrips.[Start Time], spedtrips.[Finish Time], spedtrips.[Distance], spedtrips.[Bus Type], spedtrips.[Dates], spedtrips.[Description] FROM spedtrips
UNION SELECT  2018ESY.[Name], 2018ESY.[Schools], 2018ESY.[Number Stops], 2018ESY.[Number Assigned], 2018ESY.[Duration], 2018ESY.[Start Time], 2018ESY.[Finish Time], 2018ESY.[Distance], 2018ESY.[Bus Type], 2018ESY.[Dates], 2018ESY.[Description] FROM 2018ESY;

The code works to union the regtrips and spedtrips tables, but when I add the second Union for the 2018ESY linked table, I get the following error when trying to view the query in datahseet view:

"Syntax error (missing operator) in query expression '2018ESY.[Name]'.

I have verified that all the column names and data t ypes are the same for all of these tables, so there must just be some stupid syntax error here?

I really appreciate your help!
B
 

June7

AWF VIP
Local time
Today, 14:53
Joined
Mar 9, 2014
Messages
5,470
Could simplfy by not including table name prefix in front of each field.

Enclose 2018ESY table name in [ ].
 

plog

Banishment Pending
Local time
Today, 17:53
Joined
May 11, 2011
Messages
11,643
A UNION just combines a bunch of individual SELECT statements together. Individually, each SELECT should work on its own. That is how I suggest you debug this.
 

Kheribus

Registered User.
Local time
Today, 15:53
Joined
Mar 30, 2015
Messages
97
June7, encasing the 2018ESY in brackets solved the problem. I wonder why it was necessary to bracket that table but not other tables?

I guess you're right as well that it is not necessary to include the table prefix when listing the fields.

Thanks for your quick help! I try not to come here with my problems too often but you are all so helpful when I do!

Have a great day!
 

June7

AWF VIP
Local time
Today, 14:53
Joined
Mar 9, 2014
Messages
5,470
Probably because table name begins with number.
 

Users who are viewing this thread

Top Bottom