Idempotent procedure behavior
SQL
Hard
4 views
Problem Description
Make sp_add_department safe if dept_id already exists: update name instead of failing.
Input Format
SQL procedure
Output Format
Procedure created
Sample Test Case
Output:
Procedure created
Constraints
Use upsert logic
Official Solution
CREATE PROCEDURE sp_upsert_department(IN p_id INT, IN p_name VARCHAR(100)) BEGIN UPDATE Departments SET dept_name = p_name WHERE dept_id = p_id; IF ROW_COUNT() = 0 THEN INSERT INTO Departments(dept_id, dept_name) VALUES (p_id, p_name); END IF; END;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!