Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql过程中cursol中的错误查询_Mysql_Cursor_Procedure - Fatal编程技术网

mysql过程中cursol中的错误查询

mysql过程中cursol中的错误查询,mysql,cursor,procedure,Mysql,Cursor,Procedure,我对curosr有一个问题,就是查询:从tableName中选择Crc32。我想读取Crc32列中的所有行,并根据前四个字符查找与四位数字同名的表 错误: 0 12:58:57 call ShowNewUserComments("User66") Error Code: 1146. Table 'comments.tableName' doesn't exist 呼叫程序: call ShowNewUserComments("User66"); 代码 在MySQL中,不能直接将变

我对curosr有一个问题,就是查询:从tableName中选择Crc32。我想读取Crc32列中的所有行,并根据前四个字符查找与四位数字同名的表

错误:

0   12:58:57    call ShowNewUserComments("User66")  Error Code: 1146. Table 'comments.tableName' doesn't exist
呼叫程序:

call ShowNewUserComments("User66");
代码


在MySQL中,不能直接将变量中的表名替换为查询,因此需要使用动态sql,使用
PREPARE
如下:

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 temp_table;
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET recordNotFound = 1;


    DROP TEMPORARY TABLE IF EXISTS temp_table;

    SET @query1 = CONCAT('CREATE TEMPORARY  TABLE temp_table AS
                            SELECT Crc32
                            FROM ',tableName,' ');
    PREPARE stmt FROM @query1; EXECUTE stmt; DEALLOCATE PREPARE stmt;


     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;

    DROP TEMPORARY TABLE IF EXISTS temp_table;
END

错误告诉您“comments.tableName”表不存在,因此在调用此sp之前,您需要在数据库中创建表
tableName
。是的,我知道这一点,但我在过程中声明了tableName输入值。表示例过程“User66”中的输入参数和数据库中存在的User66我希望使用set@query=(“选择Crc32 FROM”,tableName);但它对游标不起作用。另一个解决方案用途:设置tableNameUser=CONCAT(“用户”,UserId);UserId是它的输入参数,但我不能在DECLAREerror之前使用SET,而在DEALLOCATE PREPARE之后使用DECLARE。。。。我想粘贴一个错误代码,但我现在不知道我的错误。所有
DECLARE
语句必须位于顶部。尝试更新答案。
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 temp_table;
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET recordNotFound = 1;


    DROP TEMPORARY TABLE IF EXISTS temp_table;

    SET @query1 = CONCAT('CREATE TEMPORARY  TABLE temp_table AS
                            SELECT Crc32
                            FROM ',tableName,' ');
    PREPARE stmt FROM @query1; EXECUTE stmt; DEALLOCATE PREPARE stmt;


     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;

    DROP TEMPORARY TABLE IF EXISTS temp_table;
END