Solved

a massive update process to a table

Posted on 2012-03-30
8
351 Views
Last Modified: 2012-06-27
In my example i have two tables as follows

ypotrh
   id                    number
   name              varchar(50)
   aei                  varchar(10)

and another one which has the role of a convserion table (lets call it to_change)

to_change
   old_aei          varchar(10)
   new_aei         varchar(10)

i want to build an update statatement which will  replace old_aei with new_aei  in ypotrh
table (aei column)  where aei=old_aei. Please bear in mind that to_change table may contain more than one rows , so the update process should be executed for every line of to_change table.

Thanks very much
0
Comment
Question by:basilhs_s
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 16

Accepted Solution

by:
Milleniumaire earned 500 total points
ID: 37786006
Probably not the most efficient way of performing the update, but worth a try:

update ypotrh y
set y.aei = (select n.new_aei
                  from to_change n
                  where n.old_aei = y.aei)
where exists (select null
                      from to_change n2
                      where n2.old_aei = y.aei)
0
 
LVL 16

Expert Comment

by:Milleniumaire
ID: 37786012
If you have may rows to update (100's of thousands or millions) you would probably be better off re-building the table, rather than updating it.
0
 
LVL 8

Expert Comment

by:Christoffer Swanström
ID: 37786077
Instead of updating the table you could do the following:

CREATE TABLE t AS
SELECT
  t1.id
  ,t1.name
  ,t2.new_aei AS aei
FROM
  ypotrh t1
LEFT JOIN
  to_change t2
ON
  t2.aei = t1.old_aei
;

Then you can drop the old table and rename the temporary table t to ypotrh.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:basilhs_s
ID: 37786248
fortunately we are talking about a table (to_change) that contains 200 records only. I will try your solution and let you know
0
 
LVL 8

Expert Comment

by:Christoffer Swanström
ID: 37786312
I'm not sure I understand the reason for the where exists clause in Millenniumaire's suggestion. I would simply go for:

update ypotrh y
set y.aei = (select n.new_aei
                  from to_change n
                  where n.old_aei = y.aei)

And yes, for small amounts of data this will quite suffice. My solution above would be better for very large amounts of data.
0
 
LVL 16

Expert Comment

by:Milleniumaire
ID: 37786342
The where exists clause is required to restrict which rows in ypotrh are updated.

There are likely to be many rows in ypotrh with aei values that don't exist in the to_change table.  Without the where exists clause, the statement will try to update EVERY ROW in ypotrh and for those rows that don't have an old_aei value in to_change, the sub-query will not retrieve a row and this will cause a problem.
0
 
LVL 16

Expert Comment

by:Milleniumaire
ID: 37786353
I agree with tosse (as I pointed out in 37786012 above) that rather than updating a table with many, many rows it would be better to build a new table.  However, this may be more than a simple create table statement, as it may be necessary to re-create indexes, constraints and triggers on the new table.  Fortunately, in this case you don't need to do this due to the low volumes of data.
0
 

Author Closing Comment

by:basilhs_s
ID: 37805416
perfect and simple solution. thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle function to insert records? 15 62
ORA-02288: invalid OPEN mode 2 81
pivot rows to columns 1 44
minium over 4 numeric columns for each row in oracle 2 36
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

756 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