Guru E. Technology

need of every business.


How to Update a Column Value from Another Table

While working with SQL Table and queries most of the time we have to face this scenario to create or update a table with another table values. So I am going to demonstrate a simple example here.

I have a table1 which has following structure.

ID | Name | State
===================
1 | Ap | Delhi
2 | Sb | Mumbai
3 | Vb | Chennai

Now I have another table2 which has following structure.

ID | Name | Address
===================
1 | NULL | D-192, Delhi
2 | NULL | C-567, Mumbai

I would like to get updated table2 column Name from table1 column Name. Please note that I should have relationship between both table and in the above table we have common column ID which can be used to create relationship between tables.

UPDATE table2
SET T2.Name= T1.Name
FROM table2 T2
INNER JOIN table1 T1 on T1.ID=T2.ID;

It will update the table and following will be the output:

SELECT * FROM TABLE2
ID | Name | Address
===================
1 | Ap | D-192, Delhi
2 | Sb | C-567, Mumbai

Enjoy!

Posted in Developer Solutions, SQL Server


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>