执行查询时出错

时间:2014-04-22 15:15:07

标签: php mysql plsql phpmyadmin

我需要在我的数据库上执行脚本。该脚本包含一个大的PL / SQL块。 我正在尝试使用phpmyadmin来执行脚本,但我认为我做错了什么,因为我总是遇到错误。

这是我正在尝试执行的一个示例:

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;

这就是错误:

#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  

1 个答案:

答案 0 :(得分:0)

正如VMai在评论中所说,问题在于我没有使用MySQL sintax。

代码的正确sintax是:

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