Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose we have two columns, column1 and column2 from table1 and table2 respectfully. What is the SQL Query to add column1 and column2 and set the value in column1?

Help me please.
Posted
Updated 26-Feb-11 11:47am
v2

update table1 set column1 = column1 + column2 
from table2 where
table2.id = table1.id
 
Share this answer
 
Comments
Espen Harlinn 28-Feb-11 11:44am    
Something along this lines, my 5 - I'd use something like
Select column1 + column2 as newValue
from table1, table2
where table2.id = table1.id
to check that I had my "where" clause right :)
_Ashish 28-Feb-11 12:29pm    
Thanks Espen you are absolutely right, even i use this query always before final Update
 
Share this answer
 
Comments
codegeekalpha 26-Feb-11 18:12pm    
union query is just to join.. i neet arthematic addtion of two colums and then setting the value in one colume.. I.e addin Columbalance(in table1)+columndeposit(in table 2)=columbalnace(in table1)
Suppose we have two columns, column1 and column2 from table1 and table2 respectfully. What is the SQL Query to add column1 and column2 and set the value in column1?

You would need to write a SP and use Cursors to loop through all the rows of the table to do it.

Other option can be to use Table Valued Function to do the same.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900