Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update one table with data from another

Author  Topic 

bmahony993
Yak Posting Veteran

58 Posts

Posted - 2008-04-11 : 13:35:56
I want to add to a column in one table the data in another. I have tried different update statements and none work. My update statement currently looks like this:

update t1
set t1.amtYTD = t1.amtYTD + t2.Total
from #ProgramData t1 join vw_MER_Reclass t2 on (t1.ProjId = t2.ProjId and t1.task = t2.task and t1.acct = t2.acct)

I need to take the amtYTD in table1, and add to it the value of Total in table 2.

Any help would be appreciated!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-11 : 13:41:45
Are you getting any error? your query seems ok. Can you provide some sample data from your table too?
Go to Top of Page

bmahony993
Yak Posting Veteran

58 Posts

Posted - 2008-04-11 : 13:50:12
Thanks for the help!
First, I mis-spoke about what I actually wanted, but the premise is the same. I want to add the total in t2 to the AmtYTD in t1. So I chenged your code around to reflect that. I don't get any errors, but I don't get any rows affected, which I know I should.
In t1, I have a projId 1224, task 00, acct 4008, with AmtYTD of -6550.98. In t2, I have the same projId, task and acct with a total of 12,479.98. After I run my update, I would expect the AmtYTD in t1 to be 5929.00, but my AmtYTD is staying the same.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-11 : 14:11:54
Then there's definitely some problem with your matching condition. Are you sure that both the tables conatain same set of values. Also make sure you dont have any white spaces while comparing character data ( i think your fields are all int)
Go to Top of Page

bmahony993
Yak Posting Veteran

58 Posts

Posted - 2008-04-11 : 14:14:35
AArrggh! White space - good catch! I needed to trim the acct field in the second table!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-11 : 14:15:40
use LTRIM(RTRIM(Field))
Go to Top of Page
   

- Advertisement -