Solved

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

Posted on 2007-12-03
6
412 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 125 total points
ID: 20400238
Cool. Looks like you've got it.

Cheers
  David
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 49

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 49

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

840 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