Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Update Two Tables in One Statement in SQL Server?

  • Last Updated : 16 Dec, 2021

In SQL, there is a requirement of a single query/statement to simultaneously perform 2 tasks at the same time. For instance, updating 2 different tables together in a single query/statement. This involves the use of the BEGIN TRANSACTION clause and the COMMIT clause. The individual UPDATE clauses are written in between the former ones to execute both the updates simultaneously. The same is depicted in the below article. For this article, we will be using the Microsoft SQL Server as our database.

Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks.

Query:

CREATE DATABASE GeeksForGeeks

Output:

Step 2: Use the GeeksForGeeks database. For this use the below command.

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.

Syntax:

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;

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:

Step 14: Update the economics marks of student having id=2 to 86 and commerce marks of student having id=3 to 99 using a single query. This query involves updating of records belonging to the different student IDs.

Query:

BEGIN TRANSACTION;
UPDATE ECONOMICS_MARKS
SET ECONOMICS_MARKS.ECO_MARKS = 86
FROM ECONOMICS_MARKS E, COMMERCE_MARKS C
WHERE E.ID = C.ID
AND E.ID = 2;
UPDATE COMMERCE_MARKS
SET COMMERCE_MARKS.COM_MARKS = 99
FROM ECONOMICS_MARKS E, COMMERCE_MARKS C
WHERE E.ID = C.ID
AND C.ID = 3;
COMMIT;

Output:

Step 15: 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 2 is updated to 86.

Output:

Step 16: 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 3 is updated to 99.

Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!