Replacing UNION Query Parts With Queries Names

Jonny

Registered User.
Local time
Today, 14:38
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?
 
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
 
Great, thank you, Sir!
 
additional is that the columns orders should be same, otherwise the result will be unpredictable.
 
enumerate your fieldnames in the query, eg:

select Query1.column1Name, Query1.column2Name, … from Query1
UNION ALL
select Query2.column1Name, Query2.column2Name, … from Query2
 
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.
 
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
 
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

Back
Top Bottom