Mysql 在存储过程中循环预处理语句查询的结果
我有一个存储函数,它的工作是遍历和分析来自多个表的大量数据。虽然大多数表都是静态的,我可以声明游标来循环数据,但也有一些表是事先未知的,特别是语言集表(例如,Mysql 在存储过程中循环预处理语句查询的结果,mysql,loops,stored-procedures,prepared-statement,stored-functions,Mysql,Loops,Stored Procedures,Prepared Statement,Stored Functions,我有一个存储函数,它的工作是遍历和分析来自多个表的大量数据。虽然大多数表都是静态的,我可以声明游标来循环数据,但也有一些表是事先未知的,特别是语言集表(例如,language\u en,language\u fr,等等),也就是说,在编写存储函数时,我不知道这些表中的哪一个会出现 在存储函数本身中,我可以执行以下操作: declare cLang cursor for SELECT table_name FROM information_schema.tables WHERE t
language\u en
,language\u fr
,等等),也就是说,在编写存储函数时,我不知道这些表中的哪一个会出现
在存储函数本身中,我可以执行以下操作:
declare cLang cursor for
SELECT table_name FROM information_schema.tables
WHERE table_schema=database() and table_name like 'language_%';
declare continue handler for not found set exit_loop = true;
open cLang;
set exit_loop = false;
cLang_loop: loop
fetch cLang into str;
...
end loop;
这样,我就可以遍历数据库中的所有语言表。然后,我需要从它们中获取数据,并循环分析。显然,我不能为每个表声明一个游标,因为我不知道有哪些表。但我可以使用事先准备好的声明:
fetch cLang into tableName;
set @stmt_text = concat("SELECT t_code, t_string FROM ", str);
prepare stmt from @stmt_text;
execute stmt using @scId;
但是现在,我如何循环这个查询的结果呢?我最后的方式就是这样。我没有创建一个动态游标来查询表,而是使用一个预定义的临时表,并在该表上声明一个游标。然后,动态sql插入到临时表中,一个常规游标在其上进行迭代。大概是这样的:
declare cLang cursor for
SELECT table_name FROM information_schema.tables WHERE table_schema=database() and table_name like 'language_%';
declare cCode cursor for
SELECT t_code, t_string FROM tmp_lang;
declare continue handler for not found set exit_loop = true;
open cLang;
set exit_loop = false;
cLang_loop: loop
fetch cLang into str;
if exit_loop then
close cLang;
leave cLang_loop;
else
create temporary table tmp_lang (t_code varchar(50), t_string varchar(2000));
set @stmt_text = concat(
'insert into tmp_lang (t_code, t_string) SELECT t_code, t_string
from ', str);
prepare stmt from @stmt_text;
execute stmt;
if (select count(1) from tmp_lang) > 0 then
open cCode;
cCode_loop: loop
fetch cCode into lCode, lString;
if exit_loop then
close cCode;
set exit_loop = false;
leave cCode_loop;
else
-- now I can do whatever I need with the data
end if;
end loop;
end if;
deallocate prepare stmt;
drop table tmp_lang;
end if;
end loop;
我最后做这件事的方式就是这样。我没有创建一个动态游标来查询表,而是使用一个预定义的临时表,并在该表上声明一个游标。然后,动态sql插入到临时表中,一个常规游标在其上进行迭代。大概是这样的:
declare cLang cursor for
SELECT table_name FROM information_schema.tables WHERE table_schema=database() and table_name like 'language_%';
declare cCode cursor for
SELECT t_code, t_string FROM tmp_lang;
declare continue handler for not found set exit_loop = true;
open cLang;
set exit_loop = false;
cLang_loop: loop
fetch cLang into str;
if exit_loop then
close cLang;
leave cLang_loop;
else
create temporary table tmp_lang (t_code varchar(50), t_string varchar(2000));
set @stmt_text = concat(
'insert into tmp_lang (t_code, t_string) SELECT t_code, t_string
from ', str);
prepare stmt from @stmt_text;
execute stmt;
if (select count(1) from tmp_lang) > 0 then
open cCode;
cCode_loop: loop
fetch cCode into lCode, lString;
if exit_loop then
close cCode;
set exit_loop = false;
leave cCode_loop;
else
-- now I can do whatever I need with the data
end if;
end loop;
end if;
deallocate prepare stmt;
drop table tmp_lang;
end if;
end loop;
选项可以是动态光标,请参阅。选项可以是动态光标,请参阅。