Solved

one to many update help needed

Posted on 2010-09-21
5
232 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
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 70
convert in derived column 7 30
SELECT query on two levels (detail and summary) 13 50
SQL Improvement  ( Speed) 14 27
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

770 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