MySQL过程错误出现'子查询返回超过1行'
选择查询
select concat('call db_name.tmp_proc(', '\'' , TABLE_SCHEMA, '\'' ',' , '\'' , TABLE_NAME , '\'' ',30,1' ');' )
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='logdb';
result
call db_name.tmp_proc('logdb','monthly_test',30,1);
call db_name.tmp_proc('logdb','monthly_test1',30,1);
call db_name.tmp_proc('logdb','monthly_test2',30,1);
call db_name.tmp_proc('logdb','monthly_test3',30,1);
call db_name.tmp_proc('logdb','monthly_test4',30,1);
5 rows in set (0.00 sec)
创建此查询作为过程
DROP PROCEDURE test$$
CREATE PROCEDURE test() BEGIN
DECLARE result INT default 0;
SET @result = (select concat('call db_name.tmp_proc(', '\'' , TABLE_SCHEMA, '\'' ',' , '\'' , TABLE_NAME , '\'' ',30,1' ');' )
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='logdb');
PREPARE stmt1 FROM @result;
EXECUTE stmt1;
END$$
call test$$
ERROR 1242 (21000): Subquery returns more than 1 row
错误1242(21000):子查询返回超过1行
此错误已经发生并且无法执行。
有没有办法消除错误并执行所有结果查询?
============ ============ It was revised as follows with reference to the advice and performed well without errors. Thank you.
delimiter $$
DROP PROCEDURE test$$
CREATE PROCEDURE test() BEGIN
-- DECLARE result text(65535) ;
-- DECLARE cnt int default 0;
DECLARE i int default 0;
SET @cnt = (select count(*) -1 as cnt FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='logdb' AND CREATE_OPTIONS ='partitioned' ) ;
WHILE i <= @cnt DO
SET @result = (select concat('call db_name.tmp_proc(', '\'' , TABLE_SCHEMA, '\'' ',' , '\'' , TABLE_NAME , '\'' ',30,1' ')' )
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='logdb' limit i, 1) ;
PREPARE stmt1 FROM @result;
EXECUTE stmt1;
SET i = i + 1;
END WHILE;
END$$
call test$$
select query
select concat('call db_name.tmp_proc(', '\'' , TABLE_SCHEMA, '\'' ',' , '\'' , TABLE_NAME , '\'' ',30,1' ');' )
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='logdb';
result
call db_name.tmp_proc('logdb','monthly_test',30,1);
call db_name.tmp_proc('logdb','monthly_test1',30,1);
call db_name.tmp_proc('logdb','monthly_test2',30,1);
call db_name.tmp_proc('logdb','monthly_test3',30,1);
call db_name.tmp_proc('logdb','monthly_test4',30,1);
5 rows in set (0.00 sec)
Create this query as a procedure
DROP PROCEDURE test$
CREATE PROCEDURE test() BEGIN
DECLARE result INT default 0;
SET @result = (select concat('call db_name.tmp_proc(', '\'' , TABLE_SCHEMA, '\'' ',' , '\'' , TABLE_NAME , '\'' ',30,1' ');' )
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='logdb');
PREPARE stmt1 FROM @result;
EXECUTE stmt1;
END$
call test$
ERROR 1242 (21000): Subquery returns more than 1 row
ERROR 1242 (21000): Subquery returns more than 1 row
This error has occurred and cannot be executed.
Is there a way to eliminate errors and execute all queries of results?
============
============
It was revised as follows with reference to the advice and performed well without errors.
Thank you.
delimiter $
DROP PROCEDURE test$
CREATE PROCEDURE test() BEGIN
-- DECLARE result text(65535) ;
-- DECLARE cnt int default 0;
DECLARE i int default 0;
SET @cnt = (select count(*) -1 as cnt FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='logdb' AND CREATE_OPTIONS ='partitioned' ) ;
WHILE i <= @cnt DO
SET @result = (select concat('call db_name.tmp_proc(', '\'' , TABLE_SCHEMA, '\'' ',' , '\'' , TABLE_NAME , '\'' ',30,1' ')' )
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='logdb' limit i, 1) ;
PREPARE stmt1 FROM @result;
EXECUTE stmt1;
SET i = i + 1;
END WHILE;
END$
call test$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论