Solved

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

Posted on 2007-12-03
6
411 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

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.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

776 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