Solved

a massive update process to a table

Posted on 2012-03-30
8
350 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
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.

 

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

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.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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 video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

860 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