Pl Sql Bank Balance

Pl Sql Bank Balance

Pl/SQL block to withdraw a specified amount from a bank account, updating the balance. if the new balance after the withdrawal goes below 5000, raise an error.

— Enable DBMS output
SET SERVEROUTPUT ON;

DECLARE
v_balance NUMBER := 10000; — Initial balance (example)
v_withdraw NUMBER;
min_balance CONSTANT NUMBER := 5000;
BEGIN
— Accept user input
v_withdraw := &enter_amount;

-- Display input
DBMS_OUTPUT.PUT_LINE('Withdrawal Amount: ' || v_withdraw);

-- Check balance condition
IF (v_balance - v_withdraw) < min_balance THEN
    RAISE_APPLICATION_ERROR(-20001, 'Error: Minimum balance of 5000 must be maintained!');
ELSE
    v_balance := v_balance - v_withdraw;
    DBMS_OUTPUT.PUT_LINE('Withdrawal successful.');
    DBMS_OUTPUT.PUT_LINE('Remaining Balance: ' || v_balance);
END IF;

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/