Solved

UPDATE statement with INNER JOIN

Posted on 2008-06-10
7
1,599 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
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
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…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now