MYSQL While 循环中的存储过程变量
MYSQL Stored Procedure Variables in While loop
好吧,这可能非常简单,但我似乎找不到答案。
我目前正在尝试清理一些 storeod 过程,这些过程使用一堆 select 语句,其中的变量只增加数量,例如。 id1、id2、id3、id4、id5、id6、id7 和 id8。我不想连续调用超过 24 个语句,而是想将存储过程缩减为几行。
现在请原谅,如果语法有问题,不是 MySQL 程序员的职业。
在我的普通语言中,我会做一个带有变量和附加控件的 while 循环,例如。
while x <= count do
select * from table where col = id[x];
SET x = x + 1;
end;
将控件附加到变量末尾的正确方法是什么?这在存储过程中有可能吗? Select 语句比显示的多一点,但如果我能做一个像这样的简单语句,它将适用于语句的其余部分。
目前使用的代码,这只是部分代码。我只是用 3 个不同的 select 语句发布了一个。目前使用 3 种不同的基本 select into 语句。如果我能以某种方式将所有 3 条语句组合成 1 条语句,那将是惊人的,但我是 MySQL 的新手,也许再给我一些时间?
if (licount <=2 ) then /* 2 Stations */
select gen_idx into gen_idx1 from j_final_results where line=pline and station=1 and type_result=1 order by ID desc limit 1;
select gen_idx into gen_idx2 from j_final_results where line=pline and station=2 and type_result=1 order by ID desc limit 1;
set gen_idx3=0;
set gen_idx4=0;
set gen_idx5=0;
set gen_idx6=0;
set gen_idx7=0;
set gen_idx8=0;
select scan_lbl into serial1 from j_final_results where line=pline and station=1 and type_result=1 order by ID desc limit 1;
select scan_lbl into serial2 from j_final_results where line=pline and station=2 and type_result=1 order by ID desc limit 1;
set serial3='';
set serial4='';
set serial5='';
set serial6='';
set serial7='';
set serial8='';
select type_result into type1 from j_final_results where line=pline and station=1 order by ID desc limit 1;
select type_result into type2 from j_final_results where line=pline and station=2 order by ID desc limit 1;
set type3=0;
set type4=0;
set type5=0;
set type6=0;
set type7=0;
set type8=0;
您需要创建一些动态代码,我的首选方法如下所示:
WHILE X <= COUNT DO
SET @sql = CONCAT('select * from table where col = id',X);
PREPARE runme FROM @sql;
EXECUTE runme;
DEALLOCATE PREPARE runme;
SET X = X + 1;
END;
你也可以放一个 ?在@sql 字符串中并使用 USING 传递 X - 参见此处:sql-syntax-prepared-statements
好吧,这可能非常简单,但我似乎找不到答案。 我目前正在尝试清理一些 storeod 过程,这些过程使用一堆 select 语句,其中的变量只增加数量,例如。 id1、id2、id3、id4、id5、id6、id7 和 id8。我不想连续调用超过 24 个语句,而是想将存储过程缩减为几行。
现在请原谅,如果语法有问题,不是 MySQL 程序员的职业。
在我的普通语言中,我会做一个带有变量和附加控件的 while 循环,例如。
while x <= count do
select * from table where col = id[x];
SET x = x + 1;
end;
将控件附加到变量末尾的正确方法是什么?这在存储过程中有可能吗? Select 语句比显示的多一点,但如果我能做一个像这样的简单语句,它将适用于语句的其余部分。
目前使用的代码,这只是部分代码。我只是用 3 个不同的 select 语句发布了一个。目前使用 3 种不同的基本 select into 语句。如果我能以某种方式将所有 3 条语句组合成 1 条语句,那将是惊人的,但我是 MySQL 的新手,也许再给我一些时间?
if (licount <=2 ) then /* 2 Stations */
select gen_idx into gen_idx1 from j_final_results where line=pline and station=1 and type_result=1 order by ID desc limit 1;
select gen_idx into gen_idx2 from j_final_results where line=pline and station=2 and type_result=1 order by ID desc limit 1;
set gen_idx3=0;
set gen_idx4=0;
set gen_idx5=0;
set gen_idx6=0;
set gen_idx7=0;
set gen_idx8=0;
select scan_lbl into serial1 from j_final_results where line=pline and station=1 and type_result=1 order by ID desc limit 1;
select scan_lbl into serial2 from j_final_results where line=pline and station=2 and type_result=1 order by ID desc limit 1;
set serial3='';
set serial4='';
set serial5='';
set serial6='';
set serial7='';
set serial8='';
select type_result into type1 from j_final_results where line=pline and station=1 order by ID desc limit 1;
select type_result into type2 from j_final_results where line=pline and station=2 order by ID desc limit 1;
set type3=0;
set type4=0;
set type5=0;
set type6=0;
set type7=0;
set type8=0;
您需要创建一些动态代码,我的首选方法如下所示:
WHILE X <= COUNT DO
SET @sql = CONCAT('select * from table where col = id',X);
PREPARE runme FROM @sql;
EXECUTE runme;
DEALLOCATE PREPARE runme;
SET X = X + 1;
END;
你也可以放一个 ?在@sql 字符串中并使用 USING 传递 X - 参见此处:sql-syntax-prepared-statements