?
Solved

Is there an equivalent of the MERGE SQL statement in MS Acess

Posted on 2007-12-03
6
Medium Priority
?
438 Views
Last Modified: 2008-02-01
I want to add a row for a table if the key doesnt exist but update a field if the key does exist. This is accomplished easil using MERGE in Oracle, but I dont think this clause exists in Access ...?
0
Comment
Question by:fester62
  • 2
  • 2
  • 2
6 Comments
 
LVL 35

Expert Comment

by:David Todd
ID: 20399432
Hi,

Merge is coming to SQL2008, but I havent heard if Access is getting it.

The usual way around is something like this
create temp table
insert into temp table

insert into finaltable
select
from temptable
left outer join finaltable
  on finaltable.field = temptable.field
where finaltable.field is null

update ft
  set ft.field = tt.field
from finaltable ft
inner join temptable tt
  on finaltable.field = temptable.field
where
  ft.field != tt.field

HTH
  David


0
 

Author Comment

by:fester62
ID: 20399859
Thanks I can see how that would work, For a 3-field key, I assume I could modify as follows:

insert into finaltable
select
from temptable
left outer join finaltable
  on (finaltable.field1 = temptable.field1
  and finaltable.field1 = temptable.field2
  and finaltable.field3 = temptable.field3)
where finaltable.field1  is null
  and finaltable.field2 is null
  and finaltable.field3 is null;

update ft
  set ft.field4 = tt.field4
from finaltable ft
inner join temptable tt
  on finaltable.field1 = temptable.field1
   and finaltable.field2 = temptable.field2
  and finaltable.field3 = temptable.field3
where
  ft.field4 != tt.field4

Appreciate your help. Thanks again.
0
 
LVL 35

Accepted Solution

by:
David Todd earned 500 total points
ID: 20400238
Cool. Looks like you've got it.

Cheers
  David
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 52

Expert Comment

by:Gustav Brock
ID: 20401642
Actually, it is quite easy in Access and any other SQL engine.

Create a table with your updates (here tblEEfficiencyTMP) and a query like this to update/append your main table (here tblEEfficiency). Unique key is field ID here:

UPDATE tblPEEfficiency
  RIGHT JOIN tblPEEfficiencyTMP ON
    (tblPEEfficiency.ID = tblPEEfficiencyTMP.ID)
SET
  tblPEEfficiency.ID = tblPEEfficiencyTMP.ID,
  tblPEEfficiency.Date = tblPEEfficiencyTMP.Date,
  tblPEEfficiency.BOPS = tblPEEfficiencyTMP.BOPS;

Works like magic!

/gustav
0
 

Author Comment

by:fester62
ID: 20401718
Surely this only does the updates, it is not appending any records to tblEEfficiency ? I cant see how you can do it without both an INSERT and an UPDATE.
Oh I see, hold on ....the nulls on the main table would get updated to the missing values and effectively perform an INSERT.

I'll have to give that a try ...very cool if it works as you are only using one statement. Thanks gustav.

Tony
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 20401839
You are welcome!

I have used it several times and it works. Well, a bit like magic.

/gustav
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

755 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