在存储过程中使用变量作为列名?

时间:2011-09-10 05:28:12

标签: mysql mysql-error-1146

这里我写了一个mysql程序,它选择指定数据库'wp'的所有text类型字段

我希望更新每个文字类型字段行,通过 CONCAT 功能附加额外字符串

在我调用test2 后,错误显示:

Query : call test2()

Error Code : 1146
Table 'wp._tbl' doesn't exist

Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000
---------------------------------------------------

mysql将 _tbl 视为字符串,但不是变量。

我可以纠正这个吗?

代码:

DELIMITER $$
USE `wp`$$
DROP PROCEDURE IF EXISTS `test2`$$
CREATE
    PROCEDURE `test2`()
    BEGIN
    DECLARE _tbl VARCHAR(100);
    DECLARE _cl VARCHAR(100);
    DECLARE notFound INT DEFAULT 0;
    DECLARE cur CURSOR FOR SELECT table_name,column_name FROM information_schema.columns WHERE table_schema = 'wp' AND data_type ='text';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET notFound =1;
    OPEN cur;
        WHILE notFound = 0 DO
            FETCH cur INTO _tbl,_cl;
            UPDATE _tbl SET _cl = CONCAT(_cl,'extra string goes here.....');
            IF NOT noFound THEN SET notFound = 0;
            END IF;
        END WHILE;
    CLOSE cur;
    END$$

DELIMITER ;

1 个答案:

答案 0 :(得分:3)