CREATE PROCEDURE `go`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN DECLARE d_z CONDITION FOR SQLSTATE '35241';
SELECT COUNT(*)as @a from _time
IF @a>0 THEN
SIGNAL d_z SET MESSAGE_TEXT='errrrrrrrrrrrr';
END IF;
END;
错误:SQL错误(1064):您的SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以便在'_time from _time附近使用正确的语法 IF @ a> 0 THEN SIGNAL d_z SET MESSAGE_TEXT ='errrrrrrrrrr'在第9行
答案 0 :(得分:0)
您的即时错误是由格式错误的SELECT
语句引起的。您必须使用正确的SELECT INTO
语法
更改
SELECT COUNT(*)as @a from _time
到
SELECT COUNT(*) INTO @a FROM _time
^^^^
现在您的代码还存在其他几个问题:
NOT EXISTS()
用于该LIMIT 1
子句。据说,您的程序的简化版可能看起来像
DELIMITER $$
CREATE PROCEDURE `go`()
BEGIN
DECLARE d_z CONDITION FOR SQLSTATE '35241';
IF NOT EXISTS(SELECT * FROM _time) THEN
SIGNAL d_z SET MESSAGE_TEXT='errrrrrrrrrrrr';
END IF;
END$$
DELIMITER ;
这是 SQLFiddle 演示
我们试一试
mysql> CREATE TABLE _time (`id` int); Query OK, 0 rows affected (0.03 sec) mysql> DELIMITER $$ mysql> CREATE PROCEDURE `go`() -> BEGIN -> DECLARE d_z CONDITION FOR SQLSTATE '35241'; -> IF NOT EXISTS(SELECT * FROM _time) THEN -> SIGNAL d_z SET MESSAGE_TEXT='errrrrrrrrrrrr'; -> END IF; -> END$$ Query OK, 0 rows affected (0.02 sec) mysql> DELIMITER ; mysql> CALL go(); ERROR 1644 (35241): errrrrrrrrrrrr
答案 1 :(得分:0)
问题在于select语句“@a”是别名,无法进一步查询。
您可以使用peterm查询“into”而不是
希望这有帮助
快乐编码
答案 2 :(得分:0)
您有一些语法问题。以下是我的工作:
DELIMITER //
CREATE PROCEDURE `go`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE d_z CONDITION FOR SQLSTATE '35241';
select count(*) INTO @a from _time;
IF @a>0 THEN
SIGNAL d_z SET MESSAGE_TEXT='errrrrrrrrrrrr';
END IF;
END
//
重要的是: