Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
1.8k views
in SQL Queries by 11 11 15
edited by
Is it possible to update a table based on another table in SQL Server, I have two tables and I need to update all records in the second table based on matched records in the first table

How I can update a table from another table in SQL?

Thanks!

1 Answer

0 like 0 dislike
by 24 26 40
edited by

Yes, you can update a table based on another table using T-SQL Update and Inner Join Statements.

For example,

Consider you have two tables as below:

How to update table from select in SQL?

Here you can update the Name value in the first table (T1) based on the matched values in the second table (T2) using this query:

UPDATE
    t2
SET
    t2.id = t1.id,
    t2.[name] = t1.[name]
FROM
    t1  
    INNER JOIN t2  
        ON t1.id = t2.id
WHERE
    t1.name = 'ali'

Here is the result of two tables after running the above update and inner join query

pdate a table based on another table using  T-SQL

Hope it helps!

If you don’t ask, the answer is always NO!
...