Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

plsql update with join

Posted on 2014-04-08
3
Medium Priority
?
672 Views
Last Modified: 2014-04-08
Hi,

I created a update inner join as below (as per examples I've seen) and that seems to work fine

UPDATE table1 c
SET c.name =
      ( SELECT t.name
        FROM table2 t
        WHERE c.id = t.id
      )
WHERE EXISTS
        ( SELECT 1
          FROM table2 t
          WHERE c.id = t.id 
        )

Open in new window


However, that is not the whole story, since I need to be able to apply a further filter, but that filter needs to be on the table1 table. Basically that filter would look like

INNER JOIN table3 k on k.id = c.id
WHERE k.id2 = 1 and k.id3 = 2

Open in new window


so that I'm not updating the entire table1 but only a subset of table1 based on the join with table3 (if that makes sense)...

where does that third join go in this query?

thanks, Aiden
0
Comment
Question by:AidenA
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1600 total points
ID: 39985916
I had written this article:
http://www.experts-exchange.com/Database/Miscellaneous/A_1517-UPDATES-with-JOIN-for-everybody.html

anyhow, you can add indeed more EXISTS() and conditions ...
UPDATE table1 c
SET c.name =
      ( SELECT t.name
        FROM table2 t
        WHERE c.id = t.id
      )
WHERE EXISTS
        ( SELECT 1
          FROM table2 t
          WHERE c.id = t.id 
        )
   AND EXISTS 
      ( SELECT 1
             FROM  table3 k 
               WHERE k.id = c.id
                AND k.id2 = 1 
          AND k.id3 = 2
   )

Open in new window


INNER JOIN
0
 
LVL 15

Assisted Solution

by:Devinder Singh Virdi
Devinder Singh Virdi earned 400 total points
ID: 39986369
Looks like you are doing bulk update, therefore, if table is very big, you may have some kind of performance problem.
Therefore another method you can use is to create Temp table with required columns. Then use update command.
0
 

Author Closing Comment

by:AidenA
ID: 39986693
thanks for that, that seemed to work fine, thanks for link to your article, I'll take a look. Was a bit slow though, will keep your comments in mind
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Suggested Courses

824 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