0

I have created an update trigger in MySQL Community Server 5.5.16 but when I try to update statement: Update Account set credit = 100 Where Number = 14001455; I get an error "ERROR 1172 (42000): Result consisted of more than one row". I do not understand why I get this error and what's wrong with my code:

delimiter |
CREATE TRIGGER t_creditexceed AFTER UPDATE ON Account
FOR EACH ROW
BEGIN 
    DECLARE n_overdraft INTEGER;
    DECLARE n_balance INTEGER;
    DECLARE n_number INTEGER;
    DECLARE n_credit INTEGER;
    DECLARE credit_exception condition for SQLSTATE '07030';

    SELECT balance, credit, number INTO n_balance, n_credit, n_number
    FROM Account;
    IF ((n_balance < (-n_credit)) AND (n_balance >= 1.1 * (-n_credit)))
    THEN
    SET n_overdraft = n_balance + n_credit;
    INSERT INTO overdraft (account_no, over_draft) VALUES (n_number, n_overdraft);
    END IF;
    IF (n_balance < 1.1 *(- n_credit))
    THEN signal credit_exception;
    END IF;
END;
|
delimiter ;

3 Answers 3

2

This query:

SELECT balance, credit, number INTO n_balance, n_credit, n_number
FROM Account;

does not have a WHERE clause, so you'd be selecting ALL Account records into variables, which is where the error message comes from. Selecting into variables only works when you have a single result row - with multiple rows, MySQL has no way of knowing which row you want out of the result set. It won't pick for you.

Sign up to request clarification or add additional context in comments.

Comments

1

I think your issue is the statement: SELECT balance, credit, number INTO n_balance, n_credit, n_number FROM Account; - it probably returns more than 1 row, and thus MySQL cannot load the values into the variables you defined.

Not sure about your intention, but maybe you were looking for NEW. and OLD. values?

Comments

1

The initial "select balance, credit, number" is returning more than one row.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.