Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

a massive update process to a table

Posted on 2012-03-30
8
Medium Priority
?
360 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 2000 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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 …
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
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 how to recover a database from a user managed backup

688 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