Question

I need to execute a script on my database. The script contains a big PL/SQL Block. I am trying to use phpmyadmin to execute the script, but i think I am doing something wrong, becasue i always get an error.

This is an example of what i'm trying to execute:

SET AUTOCOMMIT=0;
DROP PROCEDURE IF EXISTS proceduretest;
delimiter //
CREATE PROCEDURE proceduretest()
BEGIN
    SELECT @tableexist:=COUNT(*) FROM information_schema.TABLES WHERE TABLE_NAME = 'tabletest';
    IF @tableexist > 0 THEN
        INSERT INTO docs_id_entidades VALUES(56, 'test IF TRUE', 64);
    ELSE
        INSERT INTO docs_id_entidades VALUES(56, 'test IF FALSE', 64);
    END IF;
EXCEPTION
    WHEN OTHERS THEN
        RAISE_APPLICATION_ERROR(3001, 'Some error ocurred. Implicit rollback');
END;
//
delimiter ;
CALL proceduretest();
COMMIT;
DROP PROCEDURE IF EXISTS proceduretest;

And this is the error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(3001, 'Some error ocurred. Implicit r' at line 10  
Was it helpful?

Solution

As VMai said on his comment, the problem was that I was not using MySQL sintax.

The right sintax for the code is:

SET AUTOCOMMIT=0;
DROP PROCEDURE IF EXISTS proceduretest;
delimiter //
CREATE PROCEDURE proceduretest()
BEGIN
    DECLARE tableexist INT(1);
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION ROLLBACK;
    SELECT COUNT(*) INTO tableexist FROM information_schema.TABLES WHERE TABLE_NAME = 'tabletest';
    IF tableexist > 0 THEN
        INSERT INTO docs_id_entidades VALUES(56, 'test IF TRUE', 64);
    ELSE
        INSERT INTO docs_id_entidades VALUES(56, 'test IF FALSE', 64);
    END IF;
END;
//
CALL proceduretest()//
COMMIT//
DROP PROCEDURE IF EXISTS proceduretest
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top