MySQL过程错误出现'子查询返回超过1行'

发布于 2025-01-18 09:28:02 字数 1870 浏览 1 评论 0原文

选择查询

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文