Replacing UNION Query Parts With Queries Names (1 Viewer)

Jonny

Registered User.
Local time
Today, 10:59
Joined
Aug 12, 2005
Messages
144
Two questions:
1. Could the SQL code be replaced with the query names within the UNION query.
2. Is there way to embed comments within the SQL code?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 08:59
Joined
Feb 19, 2013
Messages
16,610
1. no - although you can reference a query e.g.

SELECT * FROM qry1
UNION SELECT * FROM qry2

number of columns and datatype would need to be the same in both queries

2. In Access sql, no, but you can in TSQL and some other variants
 

Jonny

Registered User.
Local time
Today, 10:59
Joined
Aug 12, 2005
Messages
144
Great, thank you, Sir!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:59
Joined
May 7, 2009
Messages
19,241
additional is that the columns orders should be same, otherwise the result will be unpredictable.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:59
Joined
May 7, 2009
Messages
19,241
enumerate your fieldnames in the query, eg:

select Query1.column1Name, Query1.column2Name, … from Query1
UNION ALL
select Query2.column1Name, Query2.column2Name, … from Query2
 

Jonny

Registered User.
Local time
Today, 10:59
Joined
Aug 12, 2005
Messages
144
enumerate your fieldnames in the query, eg:

select Query1.column1Name, Query1.column2Name, … from Query1
UNION ALL
select Query2.column1Name, Query2.column2Name, … from Query2
Thank you , that helps.
I meant to "got an error" in positive meaning. The explained error is always preferred than unpredictable result.
 

Dreamweaver

Well-known member
Local time
Today, 08:59
Joined
Nov 28, 2005
Messages
2,466
One thing I would like to add to this topic is that BOTH sides of a UNION query can have there own WHERE Clause
Like
Code:
    StrYE = "SELECT * From QryYearlyEventsAll WHERE [EventMonth]=" & F!Cmonth & " UNION SELECT * FROM QryYearlyEvents WHERE (([EventMonth]=" & F!Cmonth & " AND [EmployeeID]=" & E & "))"


This is used in the schedule calendar of my Employees Example Here:
https://www.access-programmers.co.uk/forums/showpost.php?p=1623602
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:59
Joined
Feb 19, 2002
Messages
43,266
Union and Union All produce different results. Make sure you know what you want. Most of the time it will be Union.
 

Users who are viewing this thread

Top Bottom