Solved

update and insert records

Posted on 2009-05-09
4
220 Views
Last Modified: 2013-11-27
Hello.
I would like to be able to update existing records and insert new records into a main table from another table.

Is this possible using append query to do the inserts and update query to change the data on existing records?

I have been trying to do an append query to append new records into the main table if they don't already exist another table. Then doing an update query to update the existing data in the main table?

How may this be done or is it better doing it with a macro or code

Thanks,
Ivan
0
Comment
Question by:icarey
  • 2
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24343361
you have to do this with 2 queries.
first, the update based on the join for the existing ones, and then insert those that are not yet in the table.
the update should be easy.
the insert requires a select like this one:
select b.*
  from table b
  left outer join a on (a.key = b.key) 
   where a.key is null

Open in new window

0
 
LVL 3

Author Comment

by:icarey
ID: 24343580
thanks angelIII

I have created the query ok but am unable to insert into the table due to field count

Tables STOCK and stock_new
Field names in both
NAME TITLE NAME2

SELECT stock_new.*
FROM stock_new LEFT JOIN STOCK ON stock_new.NAME2 = STOCK.NAME2
WHERE (((STOCK.NAME2) Is Null));

displays the new records

INSERT INTO STOCK(NAME,TITLE,NAME2)
SELECT stock_new.*
FROM stock_new LEFT JOIN STOCK ON stock_new.NAME2 = STOCK.NAME2
WHERE (((STOCK.NAME2) Is Null));

comes up with an error
Number of query values and destination fields are not the same

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24343616
this will do:
INSERT INTO STOCK(NAME,TITLE,NAME2)
SELECT stock_new.Names2, stock_new.Title, stock_new.Names2
FROM stock_new LEFT JOIN STOCK ON stock_new.NAME2 = STOCK.NAME2
WHERE (((STOCK.NAME2) Is Null));

Open in new window

0
 
LVL 3

Author Closing Comment

by:icarey
ID: 31579754
Thank you angelIII your answer has help me greatly

Ivan
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

825 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