Way to abort execution of MySQL scripts (raising error perhaps)?

I had the same problem and the GUI isn't an option for me. Here's how I solved it:

    DELIMITER $$

    DROP FUNCTION IF EXISTS sfKillConnection $$

    CREATE FUNCTION sfKillConnection() RETURNS INT
    BEGIN

        SELECT connection_id() into @connectionId;
        KILL @connectionId;
        RETURN @connectionId;
    END $$


    DELIMITER ;

It's a function instead of a procedure so it can be called in a script like this:

select if(@error, sfKillConnection(), 0);

You'll probably need to start the mysql client with the --disable-reconnect option.


I think what you're encountering is a limitation of the MySQL console. Given a list of statements, the MySQL console executes each one regardless of any errors generated. Even if you implemented some of the error-raising suggestions that previous comments have mentioned, the MySQL console won't stop executing when such an error is encountered.

I'll assume that you don't have the resources to apply a scripting language to the problem that could execute your SQL for you and handle the errors. I think in this case, you just need a more robust tool than the MySQL console.

MySQL Administrator does what you need, if I understand your problem correctly. If you set up your MySQL connection and connect to the database, you have two tools available from the Tools menu. The normal MySQL console is there, but you also have the MySQL Query Browser.

If you open the Query Browser, you get a decent GUI view of your MySQL databases. File -> Open Script to open your SQL script, then use the Execute button.

You get a nice progress bar, and more importantly from the sounds of it, if a query fails, the script execution halts and highlights the failed query. You can choose to skip it and keep going, or even manually modify your data and start up from someplace else farther down the script.

I abandoned the MySQL console almost immediately once I found out about and tried Administrator.