Tuesday, 6 August 2013

SQL Server Update Multiple Tables using Inner Joins

Here I will explain SQL query to update multiple tables in SQL Server using inner joins or update multiple tables in SQL Server with joins.
Description:
In previous posts I explained difference between LEN and DATALENGTH functions in SQL Server, Convert rows to columns without pivot tables in sql server, SQL query to get data between dates and many articles relating to SQL Server. Now I will explain how to write update query with multiple tables in SQL Server.

Generally we don’t have an option to update multiple tables in single statement because of that we need to update tables separately like as shown below
Syntax to update multiple tables
UPDATE t1
SET t1.Name = 'suresh'
FROM Table1 t1, Table2 t2
WHERE t1.id = t2.id
and t1.id = 10
UPDATE t2
SET t2.username = 'dasari'
FROM Table1 t1, Table2 t2
WHERE t1.id = t2.id
and t1.id = 10
Syntax to update multiple tables with inner join
UPDATE t1
SET t1.Name = 'suresh'
FROM Table1 t1 INNER JOIN Table2 t2
and t1.id = 10
UPDATE t2
SET t2.username = 'dasari'
FROM Table1 t1 INNER JOIN Table2 t2
ON t1.id = t2.id
and t1.id = 10

No comments :

Post a Comment