Solved

a massive update process to a table

Posted on 2012-03-30
8
347 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
  • 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
 

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

708 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

13 Experts available now in Live!

Get 1:1 Help Now