CALL 上的存储过程错误
我试图调用一个编译成功的过程,但在调用时出现此错误:
查询:调用 proc5
错误代码:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行“NULL”附近使用的正确语法
这是我的存储过程:
DELIMITER $$
CREATE DEFINER = `root` @`localhost` PROCEDURE `proc5` ()
BEGIN
DECLARE done BOOL DEFAULT FALSE ;
DECLARE tablename VARCHAR (100) ;
DECLARE tracktables CURSOR FOR
SELECT
TABLE_NAME
FROM
information_schema.TABLES
WHERE TABLE_SCHEMA = 'db1' ;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = TRUE ;
OPEN tracktables ;
myloop :
LOOP
FETCH tracktables INTO tablename ;
IF done
THEN CLOSE tracktables ;
LEAVE myloop ;
END IF ;
SET @s = CONCAT(
'INSERT INTO db2.test1 SELECT * FROM ',
@tablename
) ;
PREPARE stmt1 FROM @s ;
EXECUTE stmt1 ;
DEALLOCATE PREPARE stmt1 ;
END LOOP ;
END $$
DELIMITER ;
实际上,我想从数据库中选择所有表并将这些表插入到一个表中,该表是在另一个使用 MySQL Cursors 的数据库中。当我调用这个存储过程时,出现上述错误。
I am trying to call a procedure which compiles successfully but on calling I get this error:
Query: call proc5
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1
This is my Stored procedure:
DELIMITER $
CREATE DEFINER = `root` @`localhost` PROCEDURE `proc5` ()
BEGIN
DECLARE done BOOL DEFAULT FALSE ;
DECLARE tablename VARCHAR (100) ;
DECLARE tracktables CURSOR FOR
SELECT
TABLE_NAME
FROM
information_schema.TABLES
WHERE TABLE_SCHEMA = 'db1' ;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = TRUE ;
OPEN tracktables ;
myloop :
LOOP
FETCH tracktables INTO tablename ;
IF done
THEN CLOSE tracktables ;
LEAVE myloop ;
END IF ;
SET @s = CONCAT(
'INSERT INTO db2.test1 SELECT * FROM ',
@tablename
) ;
PREPARE stmt1 FROM @s ;
EXECUTE stmt1 ;
DEALLOCATE PREPARE stmt1 ;
END LOOP ;
END $
DELIMITER ;
Actually, I want to select all the tables from a database and insert those tables into one table which is in another database using MySQL Cursors. And when I call this stored procedure I get the above error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您混合了声明的变量和即兴@vars。
变量 ->
tablename
不等于 var ->@表名
。将设置行更改为:
现在它应该可以工作了。
不需要反引号
`
,但只是为了以防万一。The problem is that you are mixing declared variables and impromtu @vars.
var ->
tablename
does not equal var ->@tablename
.Change the set line to:
Now it should work.
The backticks
`
should not be needed, but are there just in case.