Skip to content

Commit 98d1ac8

Browse files
authored
Merge pull request #372 from umara-123/main
Add update-using-select folder with three SQL Server update methods
2 parents fd127a7 + f3e8aaa commit 98d1ac8

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Method 2: Update via JOIN
2+
UPDATE Course
3+
SET is_active =
4+
CASE WHEN Department.code = 'EC' THEN 'Yes' ELSE 'No' END
5+
FROM Course
6+
JOIN Department ON Course.department_id = Department.id;
7+
8+
-- View result
9+
SELECT * FROM Course;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Method 3: Update via MERGE
2+
MERGE Course AS Target
3+
USING (
4+
SELECT id, code FROM Department
5+
) AS Source
6+
ON Target.department_id = Source.id
7+
WHEN MATCHED THEN
8+
UPDATE SET Target.is_active =
9+
CASE
10+
WHEN Source.code = 'ME' THEN 'Yes'
11+
ELSE 'No'
12+
END;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- Method 1: Update via Subquery
2+
UPDATE Course
3+
SET is_active = (
4+
SELECT CASE
5+
WHEN Department.code = 'CS' THEN 'Yes'
6+
ELSE 'No'
7+
END
8+
FROM Department
9+
WHERE Department.id = Course.department_id
10+
);
11+
12+
-- View result
13+
SELECT * FROM Course;

0 commit comments

Comments
 (0)