使用mysql中的存储过程创建更新查询

时间:2013-06-14 12:28:45

标签: mysql sql-update

我在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;

1 个答案:

答案 0 :(得分:1)

点击此链接。它给出了您怀疑的简单示例。如果您不理解链接的内容。怀着疑虑回到这个主题。

Loops in 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 ;