这是我的MySQL存储过程:
CREATE PROCEDURE myStoredProc(DB_NAME varchar(30))
BEGIN
DECLARE NO_EXAM_TABLE BOOLEAN;
/*This one throws 'No Such Table' exception*/
SELECT IF(count(*) = 0, TRUE, FALSE)
FROM information_schema.COLUMNS cols
WHERE cols.TABLE_SCHEMA = DB_NAME
AND cols.TABLE_NAME = 'exam'
INTO NO_EXAM_TABLE;
/*But this one works fine (removed the INTO clause)*/
SELECT IF(count(*) = 0, TRUE, FALSE)
FROM information_schema.COLUMNS cols
WHERE cols.TABLE_SCHEMA = DB_NAME
AND cols.TABLE_NAME = 'exam';
END;
MySQL是否认为NO_EXAM_TABLE
是一个表而不是一个变量?
修改
这就是我用来捕捉错误的内容:
DECLARE EXIT HANDLER FOR 1146 BEGIN SELECT "42S02 (ER_NO_SUCH_TABLE) Table doesn't exist" as 'ERROR_NO SQLSTATE'; ROLLBACK; END;
EDIT2
我已将问题缩小到这个条款:
AND cols.TABLE_NAME = 'exam';
如果我将其更改为此,则可以:
AND cols.TABLE_NAME LIKE 'exam';
我不明白为什么=
运算符导致错误并且LIKE
运算符正常运行。
答案 0 :(得分:-1)
您需要将INTO放在正确的位置:
select ...
into var
from table
where ...