Solved

UPDATE statement with INNER JOIN

Posted on 2008-06-10
7
1,639 Views
Last Modified: 2013-11-27
Hi,

I encountered an error when trying to use a UPDATE statement with INNER JOIN.

First let me explain to you the task I have. I have two tables tbl_A and tbl_B like this:

                   tbl_A                                                      tlb_B
caseID   fld_a    fld_b                 caseID    fld_1   fld_2   fld_3
    1          aaa     bbb                       2         ddd     eee     fff
    2           dd       ee                        4         ggg     hhh      iii

I want to INSERT values of caseID, fld_1 and fld_2 from tbl_B into fld_a and fld_b of tbl_A for those caseIDs that tbl_A does not have. However, I want to UPDATE records in tbl_A with values from tbl_B for those caseID that also in tbl_B. So, the result should look like this:

                   tbl_A                                                  
caseID   fld_a    fld_b                
    1          aaa     bbb                
    2          ddd     eee                    
    4          ggg     hhh

Note here caseID in tbl_A is the primary key. No duplicates are allowed.

I can use INSERT INTO statement to insert records. It works fine. But when I tried the following UPDATE statement with INNER JOIN, error occurs.

UPDATE tbl_A SET tbl_A.fld_a = tbl_B.fld_1, tbl_A.fld_b = tbl_B.fld_2 FROM tlb_A INNER JOIN tbl_B ON tbl_A.caseID = tbl_B.caseID

Can anyone tell me where goes wrong? Or do you have any better way to achieve the task that I mentioned?

Thank you very much.
0
Comment
Question by:angie_angie
[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
7 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 21757467
try like this

UPDATE tbl_A SET tbl_A.fld_a = tbl_B.fld_1, tbl_A.fld_b = tbl_B.fld_2 FROM tlb_A , tbl_B

where  tbl_A.caseID = tbl_B.caseID
0
 

Author Comment

by:angie_angie
ID: 21757490
pratima mcs,

I tried yours. It doesn't seem to work though...
0
 

Author Comment

by:angie_angie
ID: 21757503
error is

Run-time error '3144': Syntax error in UPDATE statement.

Btw, I did it in a Sub by using CurrentDb.Execute sGetSQL and sGetSQL is the UPDATE statement... Could it be the problem?
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 19

Accepted Solution

by:
frankytee earned 500 total points
ID: 21757537
UPDATE
tlb_A INNER JOIN tbl_B ON tbl_A.caseID = tbl_B.caseID
SET tbl_A.fld_a = tbl_B.fld_1, tbl_A.fld_b = tbl_B.fld_2
0
 
LVL 22

Expert Comment

by:dportas
ID: 21757565
What is / are the key(s) of tbl_B? If caseID is not unique in tbl_B then the result would be that the two columns get updated from some undetermined row where tbl_A.caseID = tbl_B.caseID. Unpredictable results are not usually desirable and may indicate a flaw in your requirements. Unfortunately you didn't specify a key in tbl_B so we can only guess.
0
 

Author Comment

by:angie_angie
ID: 21757624
dportas,

The caseID in both tables are unique.
0
 
LVL 77

Expert Comment

by:peter57r
ID: 21757850
frankytee has posted the correct answer.....
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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