Solved

one to many update help needed

Posted on 2010-09-21
5
255 Views
Last Modified: 2012-05-10
Hello, I'm attempting to use the following to update a table from another table. This is a one to many update.

When I run this only one row is updated which is not correct.

update a
set a.custnmbr = b.custnmbr
From BB_TNCommFac3_LocationsBScott a, cc_scott b
where ltrim(rtrim(b.oldid)) = ltrim(rtrim(a.oldid))

this brings 1149 rows
select * from cc_scott where ltrim(rtrim(oldid)) in (select ltrim(rtrim(oldid)) from BB_TNCommFac3_LocationsBScott)




0
Comment
Question by:gogetsome
[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
5 Comments
 
LVL 2

Accepted Solution

by:
mgrabarz earned 500 total points
ID: 33726083
select * from cc_scott where ltrim(rtrim(oldid)) in (select ltrim(rtrim(oldid)) from BB_TNCommFac3_LocationsBScott)
This query returns rows from cc_scott  table that have the value of oldid existing in BB_TNCommFac3_LocationsBScott. Maybe there are 1149 rows with the same oldid value?

In update process you will update the same BB_TNCommFac3_LocationsBScott row for 1149 times...
0
 
LVL 2

Expert Comment

by:Ahrensy
ID: 33726093
I would do something like:
update table 1 set table1.field = (select table2.field from table2 where table1.id = table2.id)

Open in new window

0
 

Author Comment

by:gogetsome
ID: 33726141
Yes, I need to update the custnmbr on  BB_TNCommFac3_LocationsBScott with the custnmbr on cc_scott based on the oldid that resides in both tables.

cc_scott is the billing table and  BB_TNCommFac3_LocationsBScott is the shipping table. There are more shipping address then billing addresses. All shipping addresses need the same custnmbr as their corresponding billing custnmbr.

The cc_scott table has 1149 rows and the  BB_TNCommFac3_LocationsBScott table has 1909 rows.
0
 
LVL 1

Expert Comment

by:Dmapros
ID: 33726395
I typically use this format when updating items based on data from another table:

UPDATE <tblname>
SET <colname> =
SELECT <colname with data for update>
0
 
LVL 1

Expert Comment

by:Dmapros
ID: 33726417
Sorry last post was not complete when it uploaded.

 typically use this format when updating items based on data from another table:

UPDATE <tblname>
SET <colname> =
SELECT <colname with data for update>
FROM <tablename>
WHERE <conditions>

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

751 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