Procedure with transaction inside
SQL
Medium
4 views
Problem Description
Create sp_transfer_funds(p_from, p_to, p_amt) to transfer money between two accounts. If the sender does not have enough balance, rollback the transaction so nothing changes.
Input Format
SQL procedure
Output Format
Procedure created
Sample Test Case
Input:
Accounts(acc_id, balance)
Output:
Procedure created
Constraints
MySQL-style procedure syntax
Official Solution
CREATE PROCEDURE sp_transfer_funds(IN p_from INT, IN p_to INT, IN p_amt DECIMAL(10,2)) BEGIN START TRANSACTION; UPDATE Accounts SET balance = balance - p_amt WHERE acc_id = p_from AND balance >= p_amt; IF ROW_COUNT() = 0 THEN ROLLBACK; ELSE UPDATE Accounts SET balance = balance + p_amt WHERE acc_id = p_to; COMMIT; END IF; END;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!