[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2007-12-03
6
Medium Priority
?
435 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

656 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