How to Update Two Tables in One Statement in SQL Server?
Last Updated :
31 May, 2024
To update two tables in one statement in SQL Server, use the BEGIN TRANSACTION clause and the COMMIT clause. The individual UPDATE clauses are written in between the former ones to execute both updates simultaneously.
Here, we will learn how to update two tables in a single statement in SQL Server.
Syntax
Updating two tables in one statement in SQL Server syntax is:
BEGIN TRANSACTION;
UPDATE TABLE_1
SET TABLE_1.TABLE_1_COLUMN = VALUE_1
FROM TABLE_1 T1, TABLE_2 T2
WHERE T1.ID = T2.ID
AND T1.ID = ID_VALUE_1;
UPDATE TABLE_2
SET TABLE_2.TABLE_2_COLUMN = VALUE_2
FROM TABLE_1 T1, TABLE_2 T2
WHERE T1.ID = T2.ID
AND T2.ID = ID_VALUE_2;
COMMIT;
Steps to Update Two Tables in One Statement in SQL Server
Follow this step-by-step guide, where we create two tables and update them in one statement.
Step 1: Create a Database.
Query:
CREATE DATABASE GeeksForGeeks
Output:

Step 2: Use the database.
Query:
USE GeeksForGeeks
Output:

Step 3: Create a table ECONOMICS_MARKS inside the database GeeksForGeeks. This table has 3 columns namely ID, S_NAME and ECO_MARKS containing the roll number and name of the students and the marks scored by the students in economics subject.
Query:
CREATE TABLE ECONOMICS_MARKS(
ID INT,
S_NAME VARCHAR(10),
ECO_MARKS INT);
Output:

Step 4: Describe the structure of the table ECONOMICS_MARKS.
Query:
EXEC SP_COLUMNS ECONOMICS_MARKS;
Output:

Step 5: Create a table COMMERCE_MARKS inside the database GeeksForGeeks. This table has 3 columns namely ID, S_NAME and COM_MARKS containing the roll number and name of the students and the marks scored by the students in commerce subject.
Query:
CREATE TABLE COMMERCE_MARKS(
ID INT,
S_NAME VARCHAR(10),
COM_MARKS INT);
Output:

Step 6: Describe the structure of the table COMMERCE_MARKS.
Query:
EXEC SP_COLUMNS COMMERCE_MARKS;
Output:

Step 7: Insert 5 rows into the ECONOMICS_MARKS table.
Query:
INSERT INTO ECONOMICS_MARKS VALUES (1,'SAM',70);
INSERT INTO ECONOMICS_MARKS VALUES (2,'AMY',68);
INSERT INTO ECONOMICS_MARKS VALUES (3,'EMMA',69);
INSERT INTO ECONOMICS_MARKS VALUES (4,'ROB',57);
INSERT INTO ECONOMICS_MARKS VALUES (5,'KEVIN',65);
Output:

Step 8: Display all the rows of the ECONOMICS_MARKS table.
Query:
SELECT * FROM ECONOMICS_MARKS;
Output:

Step 9: Insert 5 rows into the COMMERCE_MARKS table.
Query:
INSERT INTO COMMERCE_MARKS VALUES (1,'SAM',80);
INSERT INTO COMMERCE_MARKS VALUES (2,'AMY',88);
INSERT INTO COMMERCE_MARKS VALUES (3,'EMMA',90);
INSERT INTO COMMERCE_MARKS VALUES (4,'ROB',75);
INSERT INTO COMMERCE_MARKS VALUES (5,'KEVIN',56);
Output:

Step 10: Display all the rows of the COMMERCE_MARKS table.
Query:
SELECT * FROM COMMERCE_MARKS;
Output:

Step 11: Update the economics and commerce marks of student having id=1 to 80 and 75 respectively using a single query. This involves 2 steps. First, perform JOIN of ECONOMICS_MARKS table and COMMERCE_MARKS table. Then using ALIASES of the tables which are E and C respectively, compare the ID of students(to ensure same ID is picked from both tables) AND finally compare the student ID to 1(given value). When both these conditions fulfill, UPDATE the corresponding marks to 80 and 75. This whole thing must be enclosed between BEGIN TRANSACTION and COMMIT to treat it a single ATOMIC operation. This query involves updating of records belonging to the same student IDs.
Query:
BEGIN TRANSACTION;
UPDATE ECONOMICS_MARKS
SET ECONOMICS_MARKS.ECO_MARKS = 80
FROM ECONOMICS_MARKS E, COMMERCE_MARKS C
WHERE E.ID = C.ID
AND E.ID = 1;
UPDATE COMMERCE_MARKS
SET COMMERCE_MARKS.COM_MARKS = 75
FROM ECONOMICS_MARKS E, COMMERCE_MARKS C
WHERE E.ID = C.ID
AND C.ID = 1;
COMMIT;
Output:

Step 12: Display all the rows of the updated ECONOMICS_MARKS table.
Query:
SELECT * FROM ECONOMICS_MARKS;
Note - The value of the column ECO_MARKS for the ID 1 is updated to 80.
Output:

Step 13: Display all the rows of the updated COMMERCE_MARKS table.
Query:
SELECT * FROM COMMERCE_MARKS;
Note: The value of the column COM_MARKS for the ID 1 is updated to 75.
Output:

Similar Reads
How to Update a Column in a Table in SQL Server
In the database management area, updating columns in a table is a normal query and it is important software that ensures the accuracy and integrity of data. Whether we are making spelling corrections, editing or altering existing information, or being attentive to changing requirements, carrying out
4 min read
How to Update Table Rows in SQL Server using Subquery ?
Updating table rows in SQL Server using a subquery is a common operation that allows us to modify records based on values derived from another table or query. In this article, we will explain how to update table rows in SQL Server using subquery with the help of examples. Updating Table Rows Using a
3 min read
Multi-Statement Table Valued Function in SQL Server
In SQL Server, a multi-statement table-valued function (TVF) is a user-defined function that returns a table of rows and columns. Unlike a scalar function, which returns a single value, a TVF can return multiple rows and columns. Multi-statement function is very much similar to inline functions only
4 min read
How to Update Multiple Columns in Single Update Statement in SQL?
The SQL UPDATE statement is a important operation for modifying existing records in a database table. It allows us to change the values of one or more columns in a table based on specific conditions. In many cases, we may need to update multiple columns in a single operation to keep our data consist
4 min read
How to update existing table rows in SQLAlchemy in Python?
In this article, we are going to see how to use the UPDATE statement in SQLAlchemy against a PostgreSQL database in Python. Creating table for demonstration:Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database using create_engine() function as sho
3 min read
How to Update Top 100 Records in SQL Server
SQL Server is a Relational database Management system which is developed by Microsoft and is one of the most used databases in the world. It provides the developer with a set of rich functionalities to create tables, insert data in them, and then manipulate and play with them as and when necessary.
5 min read
How to Update Records in Table from CTE in SQL
Common Table Expressions (CTEs) in SQL is an important feature that provides a temporary result set that can be referred to within a SELECT, INSERT, UPDATE, or DELETE statement. In this article, we will learn how to use CTEs to update records in SQL along with examples along with an explanation.What
4 min read
What is Nested Select Statement in SQL Server
SQL Server is a powerful relational database management system. Sql Server is very good with its robustness and scalability. SQL Server operates as a client-server structure, imparting centralized management and scalability for huge-scale applications and enterprise-stage solutions. It offers advanc
3 min read
How to Update Table Rows in PL/SQL Using Subquery?
Updating table rows in PL/SQL via subqueries allows precise data modifications based on specific conditions. By integrating subqueries within the UPDATE statement, rows can be selectively targeted for updates, enhancing data management efficiency. This article explores the concept of updating rows i
4 min read
SQL Server Update From One Table to Another Based on an ID Match
In the world of database management, we need to perform various OLTP operations like insert, update, and delete. The ability to efficiently update data between tables is crucial for maintaining data integrity and ensuring accurate information. SQL Server provides powerful tools to accomplish this ta
8 min read