这是我的 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;
编辑2
我已将问题缩小到此条款:
AND cols.TABLE_NAME = 'exam';
如果我将其更改为此,它将起作用:
AND cols.TABLE_NAME LIKE 'exam';
我不明白为什么=
操作员会导致错误并且LIKE
操作员工作正常。