How to use CASE Statement In SQL
Step 6: You can also use the the keyword UPDATE and WHEN to update the values. For example a condition within the CASE statement. If the value in column_name matches ‘column_name1’, then it updates column_value to column_value1. ELSE column_value: This part of the CASE statement specifies the default value for column_value if none of the previous conditions are met. END: This closes the CASE statement.
Syntax:
UPDATE table_name
SET column_name
= CASE column_name
WHEN 'column_name1' THEN column_value1
WHEN 'column_name2' THEN column_value2
ELSE column_value
END
WHERE column_name IN ('column_name1', 'column_name2');
Query:
UPDATE Department
SET Position =
CASE employee_id
WHEN 101 THEN 'Senior Software Engineer'
WHEN 102 THEN 'Senior Data Analyst'
WHEN 103 THEN 'Senior Product Manager'
ELSE 'Position Not Uptated'
END;
Output:
How to Update Multiple Rows in PostgreSQL?
In PostgreSQL, updating multiple rows in a single query is done using the UPDATE keyword. We appoint two sorts of examples: the primary includes updating based totally on a single condition, while the second relates to updating based totally on multiple conditions. Throughout this article, we can make use of PostgreSQL Server as our database and reference the SELECT keyword for illustrative purposes.