Solved

Takes results from union query and append to table

Posted on 2011-09-21
5
240 Views
Last Modified: 2012-05-12
reference q #
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_27320023.html

I have a third table that needs the data from the query below inserted into it.
Table3

3 fields same field names
fld1 PrimaryKey Dups-ok
fld2 PrimaryKey Dups-ok
fld3 PrimaryKey Dups-ok

What I need:
I need to take the results from this query below and append the data to a new table...("Table3")
same structure as the other 2 tables  "Table1" and "Table2"
Table1
Table2

3 fields in each table  same field names
fld1 PrimaryKey Dups-ok
fld2 PrimaryKey Dups-ok
fld3 PrimaryKey Dups-ok

Thanks
fordraiders


Select A.* From
(
SELECT Table1.fld1, Table1.fld2, Table1.fld3,"Table1" as SourceTable
FROM Table1 LEFT JOIN Table2 ON (Table1.fld3 = Table2.fld3) AND (Table1.fld2 = Table2.fld2) AND (Table1.fld1 = Table2.fld1)
WHERE (((Table2.fld1) Is Null) AND ((Table2.fld2) Is Null) AND ((Table2.fld3) Is Null))

Union All
SELECT Table2.fld1, Table2.fld2, Table2.fld3, "table2"
FROM Table2 LEFT JOIN Table1 ON (Table2.fld3 = Table1.fld3) AND (Table2.fld2 = Table1.fld2) AND (Table2.fld1 = Table1.fld1)
WHERE (((Table1.fld1) Is Null) AND ((Table1.fld2) Is Null) AND ((Table1.fld3) Is Null))
) As A

Open in new window

0
Comment
Question by:fordraiders
  • 3
  • 2
5 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36577316
insert into table3 (fld1,fld2,fld3)
select A.fld1,A.fld2,A.fld3
from
(
SELECT Table1.fld1, Table1.fld2, Table1.fld3,"Table1" as SourceTable
FROM Table1 LEFT JOIN Table2 ON (Table1.fld3 = Table2.fld3) AND (Table1.fld2 = Table2.fld2) AND (Table1.fld1 = Table2.fld1)
WHERE (((Table2.fld1) Is Null) AND ((Table2.fld2) Is Null) AND ((Table2.fld3) Is Null))

Union All
SELECT Table2.fld1, Table2.fld2, Table2.fld3, "table2"
FROM Table2 LEFT JOIN Table1 ON (Table2.fld3 = Table1.fld3) AND (Table2.fld2 = Table1.fld2) AND (Table2.fld1 = Table1.fld1)
WHERE (((Table1.fld1) Is Null) AND ((Table1.fld2) Is Null) AND ((Table1.fld3) Is Null))
) As A
0
 
LVL 3

Author Comment

by:fordraiders
ID: 36578585
cap, taking your example and inserting my real field names

see attached text file of append with real fields.

error:
Msg 207, Level 16, State 1, Line 6
Invalid column name 'CRS_DescmatchGisXrefExt_staging'.
Msg 207, Level 16, State 1, Line 10
Invalid column name 'CRS_DescmatchGisXrefExt'.

using format below as line reference:
1. INSERT INTO
2. SELECT A.item
3.
4. FROM
5.  (
6. SELECT
7. FROM
8. WHERE
9. UNION ALL
10 .SELECT
11. FROM
12. WHERE
13. ) A



I think its a quotes issue but please take a look ?
 

unionquery-append-capexample.txt
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 36580244
fordraiders,

the sql in the text file you uploaded has this line broken


WHERE (((CRS_DescmatchGisXrefExt.item) Is Null) AND ((CRS_Descmatch



GisXrefExt.vendor_name) Is Null) AND ((CRS_DescmatchGisXrefExt.mfrnum) Is Null))
Union All


try correcting it first, like this



WHERE (((CRS_DescmatchGisXrefExt.item) Is Null) AND ((CRS_DescmatchGisXrefExt.vendor_name) Is Null) AND ((CRS_DescmatchGisXrefExt.mfrnum) Is Null))
Union All
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 36580314
Thanks...I also had to change the " marks  because I was putting the sql in sql server 2008 r2...
Thanks
for the help
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36580361
actually, since you are not using the "sourceTable" in your append query,
you can remove this parts

   ,"CRS_DescmatchGisXrefExt_staging" as SourceTable


   , "table2"


from the sql statment
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
combine ShipTo and BillTo Address 3 23
Sub Reports 8 23
Is it possible to reset DSum? 12 43
What logic to build in order to get a weekly reminder 9 42
In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now