Using an Append or Update Query to insert data into a table (Using 2 tables)

Sawduster

New Member
Joined
Aug 18, 2016
Messages
16
Iam try to create an append query, in order to, insert records into an already existing field (using 2 different tables).

Basically, I want to insert the dates from a column (field) labeled “GraduationDate” that is located in a table labeled “OrderHeader” into a table labeled “OrderDeliveryAlt” in a column (field) also labeled “GraduationDate” but only update that field that contains the same “OrderNu” as the date.

For example, everywhere there is the OrderNu “T0020204” with the graduation date of 5/15/2018 (in the OrderHeader table) I want that graduation date to be inserted into the graduation date in the OrderDeliveryAlt table with that corresponding OrderNu.

Here is what I have but it doesn’t work:

INSERT INTO OrderDeliveryAlt ( GraduationDate )
SELECT OrderHeader.GraduationDate
FROM OrderHeader;

Which is the correct SQL code to use in this case? Should I use an append query or Update Query?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You are using the wrong query type.

An Append Query is used to Insert brand new records into a Table.
An Update Query is used to Update fields on existing records in a Table.

It sounds to me like you are updating existing records, so want an Update Query.
Join the two tables on your OrderNu value, then update the Graduation Date field.

See here: https://support.office.com/en-us/ar...te-query-9dddc97c-f17d-43f4-a729-35e5ee1e0514
 
Upvote 0
Ok I tried this code however it didn't work. How can this be corrected?

UPDATE OrderHeader INNER JOIN OrderDeliveryAlt ON OrderHeader.OrderNu = OrderDeliveryAlt.OrderNu SET OrderDeliveryAlt.GraduationDate = "GraduationDate";
 
Upvote 0
If you are UPDATING the "OrderHeader" table, why is your SET command referencing a field from the "OrderDeliveryAlt" table?
These two tables should be the same.

And why are you trying to set the field equal to the Text string "GraduationDate", instead of the GraduationDate field (anything enclosed in double-quotes is treated as literal text)?

It should be something like:
Code:
[COLOR=#333333]UPDATE OrderHeader 
INNER JOIN OrderDeliveryAlt ON OrderHeader.OrderNu = OrderDeliveryAlt.OrderNu 
SET [/COLOR][COLOR=#333333]OrderHeader.[/COLOR][COLOR=#333333]GraduationDate=[/COLOR][COLOR=#333333]OrderDeliveryAlt.GraduationDate;[/COLOR]
 
Upvote 0
Graduation Date is a Column name, not a string. Also you are updating the wrong table:
Code:
UPDATE 
    [B]OrderDeliveryAlt [/B]
    INNER JOIN OrderHeader 
    ON OrderDeliveryAlt.OrderNu = OrderHeader.OrderNu 
SET 
    OrderDeliveryAlt.[B]GraduationDate [/B]= OrderHeader.[B]GraduationDate[/B];
Also as a rule I'd only update null dates or possibly dates that differ, on the premise that you should never be updating things that don't need to be updated:
Code:
UPDATE 
    OrderDeliveryAlt 
    INNER JOIN OrderHeader 
    ON OrderDeliveryAlt.OrderNu = OrderHeader.OrderNu 
SET 
    OrderDeliveryAlt.GraduationDate = OrderHeader.GraduationDate
WHERE
    OrderDeliveryAlt IS NULL;
or
Code:
UPDATE 
    OrderDeliveryAlt 
    INNER JOIN OrderHeader 
    ON OrderDeliveryAlt.OrderNu = OrderHeader.OrderNu 
SET 
    OrderDeliveryAlt.GraduationDate = OrderHeader.GraduationDate
WHERE
    Nz(OrderDeliveryAlt.GraduationDate ,#1/1/1970#) <> OrderHeader.GraduationDate;

Although it wouldn't actually hurt to update a value to what it already is, generally speaking, I would still try to avoid this, especially considering this query will essentially be updating all of your records all the time otherwise, and that will degrade performance over time unless it's a one-off action.

Note that I used to believe that in Access the table you are updating must come first in a join clause, but now I really don't know anymore if I was correct or not. Access is a bit more finicky when it comes to updates and deletes than other database engines.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,179
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top