2

我在 oracle 中创建了存储过程。但是我想在 mysql 中运行这个过程,如何在 mysql 中创建 for 循环。我对 mysql 中的 for 循环一无所知。

Create table student
  (
    Id number,
    Batch varchar2(2),
    Batch_roll_no number 
  )

Create or replace procedure assign_roll_no
Is
 V_roll_no number; 
Begin
For c1 in (select distinct batch from student order by 1)
Loop
Select nvl(Max(batch_roll_no+1),1)
Into v_roll_no
From student
 where batch = c1.batch;
  For c2 in ( select id from batch where batch_roll_no is null and batch= c1.batch
 Order by 1)
 Loop
Update student
set batch_roll_no=v_roll_no
Where id=c2.id
And batch= c1.batch;
V_roll_no:=v_roll_no + 1;
End loop;
End loop;
End;
4

1 回答 1

1

按照这个链接。它为您的疑问提供了简单的示例。以防万一,您不了解链接的内容。带着你的疑问回到这个话题。

MySQL 中的循环

就答案而言,我希望以下内容能让 Pravin 清楚地了解您正在查看的场景。

DELIMITER $$ 
CREATE PROCEDURE loop_test() 
BEGIN  
WHILE c2 in (select id from batch where batch_roll_no is null and batch= c1.batch Order by 1)
DO  
Update student  
SET batch_roll_no=v_roll_no  
WHERE id=c2.id AND batch= c1.batch;  
V_roll_no:=v_roll_no + 1;  
END WHILE;  
END$$
DELIMITER ;
于 2013-10-04T11:52:35.637 回答