在存储过程中使用游标循环行 MySQL
Using a cursor in a stored procedure to loop rows MySQL
场景: 我有一个存储过程,它根据 2 个输入从 table 获取数据:一个日期和一个字符串(这是一个列名)。第一个过程是从另一个过程调用的,该过程使用游标循环遍历 table 的行并将每一行传递给第一个过程的字符串(要检查的列名)。我对第二个程序(即要直接调用的程序)的输入是日期。
问题: 我的第一个过程 运行 当我自己调用时没问题。我的第二个过程抛出了一些我不知道如何修复的语法错误。
Obs: 我已经在此处检查了有关此主题的其他一些答案
比如Using Cursor in a Loop of a stored procedure and How can I loop through all rows of a table? (MySQL) . Actually my second procedure is now a modified version of a query I found on SE https://dba.stackexchange.com/questions/138549/mysql-loop-through-a-table-running-a-stored-procedure-on-each-entry
问题: 目前,代码在我对 @colval 的声明中的第 5 行抛出错误。
代码:
-- Procedure for looping through rows of `wanted_columns` table:
delimiter $$
drop procedure if exists `data_check_loop` $$
create procedure `data_check_loop`(`wanted_date` date)
begin
set @dateval = `wanted_date`;
declare colval string default null;
-- boolean variable to indicate cursor is out of data
declare done tinyint default false;
-- declare a cursor to select the desired columns from the desired source table
declare cursor1
cursor for
select t1.c1
from `wanted_columns` t1;
-- catch exceptions
declare continue handler for not found set done = true;
-- open the cursor
open cursor1;
my_loop:
loop
fetch next from cursor1 into colval;
if done then
leave my_loop;
else
call `set_column_stats`(colval, dateval);
end if;
end loop;
close cursor1;
end $$
delimiter ;
问题:关于如何解决这个问题有什么想法吗?
你的程序有几个问题。首先,如 manual:
中所述
DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.
所以你需要移动你的
set @dateval = `wanted_date`;
在所有 DECLARE
之后(包括游标和继续处理程序)。
其次,您对 colval
的声明不正确,string
不是有效的数据类型,应替换为 text
:
declare colval text default null;
场景: 我有一个存储过程,它根据 2 个输入从 table 获取数据:一个日期和一个字符串(这是一个列名)。第一个过程是从另一个过程调用的,该过程使用游标循环遍历 table 的行并将每一行传递给第一个过程的字符串(要检查的列名)。我对第二个程序(即要直接调用的程序)的输入是日期。
问题: 我的第一个过程 运行 当我自己调用时没问题。我的第二个过程抛出了一些我不知道如何修复的语法错误。
Obs: 我已经在此处检查了有关此主题的其他一些答案 比如Using Cursor in a Loop of a stored procedure and How can I loop through all rows of a table? (MySQL) . Actually my second procedure is now a modified version of a query I found on SE https://dba.stackexchange.com/questions/138549/mysql-loop-through-a-table-running-a-stored-procedure-on-each-entry
问题: 目前,代码在我对 @colval 的声明中的第 5 行抛出错误。
代码:
-- Procedure for looping through rows of `wanted_columns` table:
delimiter $$
drop procedure if exists `data_check_loop` $$
create procedure `data_check_loop`(`wanted_date` date)
begin
set @dateval = `wanted_date`;
declare colval string default null;
-- boolean variable to indicate cursor is out of data
declare done tinyint default false;
-- declare a cursor to select the desired columns from the desired source table
declare cursor1
cursor for
select t1.c1
from `wanted_columns` t1;
-- catch exceptions
declare continue handler for not found set done = true;
-- open the cursor
open cursor1;
my_loop:
loop
fetch next from cursor1 into colval;
if done then
leave my_loop;
else
call `set_column_stats`(colval, dateval);
end if;
end loop;
close cursor1;
end $$
delimiter ;
问题:关于如何解决这个问题有什么想法吗?
你的程序有几个问题。首先,如 manual:
中所述DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.
所以你需要移动你的
set @dateval = `wanted_date`;
在所有 DECLARE
之后(包括游标和继续处理程序)。
其次,您对 colval
的声明不正确,string
不是有效的数据类型,应替换为 text
:
declare colval text default null;