【发布时间】:2012-08-23 11:00:05
【问题描述】:
我对 curosr 有疑问,完全是查询:SELECT Crc32 FROM tableName。我想从 Crc32 列中读取所有行,并根据前四个字符找到一个与四个数字同名的表
错误:
0 12:58:57 call ShowNewUserComments("User66") Error Code: 1146. Table 'comments.tableName' doesn't exist
调用程序:
call ShowNewUserComments("User66");
代码
DELIMITER $$
DROP PROCEDURE IF EXiSTS ShowNewUserComments $$
CREATE PROCEDURE `ShowNewUserComments`(tableName varchar(255)/*, Date TIMESTAMP*/)
BEGIN
DECLARE recordNotFound INTEGER DEFAULT 0;
DECLARE oneRow VARCHAR(10) DEFAULT "";
DECLARE getCrc32,i int (11);
DECLARE myCursor CURSOR FOR SELECT Crc32 FROM tableName;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET recordNotFound = 1;
OPEN myCursor;
set i = 0;
allRows: LOOP
FETCH myCursor INTO oneRow;
set i = i + 1;
IF recordNotFound THEN
LEAVE allRows;
END IF;
END LOOP allRows;
CLOSE myCursor;
END
【问题讨论】:
-
错误告诉你表'cmets.tableName'不存在所以在调用这个sp之前你需要在你的数据库
comments中创建表tableName。 -
是的,我知道这一点,但我在过程中声明了 tableName 输入值。程序“User66”和User66中的表示例输入参数存在于数据库中
-
我想使用 set @query = ("SELECT Crc32 FROM ", tableName);但不适用于光标。另一种解决方案使用: set tableNameUser = CONCAT("User",UserId);和 UserId 它的输入参数但是我不能在单词 DECLARE 之前使用“SET”