Solved

a massive update process to a table

Posted on 2012-03-30
8
355 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
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…
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
Suggested Courses

628 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