[英]create update query using stored procedure in mysql
我已經在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;
點擊此鏈接。 它提供了一些簡單的示例供您懷疑。 如果您不理解鏈接的內容。 帶着疑問回到這個話題。
就答案而言,我希望以下內容可以使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 ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.