我需要在我的数据库上执行一个脚本。该脚本包含一个大的 PL/SQL block 。 我正在尝试使用 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
最佳答案
正如 VMai 在他的评论中所说,问题是我没有使用 MySQL 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
关于php - 执行查询时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23223522/