MySql Cursor - Creating a procedure

后端 未结 3 787
温柔的废话
温柔的废话 2021-01-06 00:12

i\'m trying to create a cursor for the first time. I have looked at the documentation, i understand the concept, but i can\'t seem to get it to even be declared...

I

3条回答
  •  长发绾君心
    2021-01-06 00:50

    create or replace procedure cursor_sample()
    BEGIN
    
    DECLARE done int default 0 ;
    DECLARE data1 varchar(20) ;
    DECLARE data2 int ;
    
    DECLARE cur1 CURSOR FOR 
    select sname,examscore from student ;
    DECLARE CONTINUE HANDLER 
    FOR NOT FOUND SET done = 1 ;
    OPEN cur1;
    loop1:LOOP 
        FETCH cur1 into data1,data2 ;
        insert into student_log(user_name,score) values (data1,data2) ;
        if done = 1 THEN
            LEAVE loop1 ;
        END IF ;
    END LOOP;
    CLOSE cur1;
    END ;
    //
    

    --This is an example for a Proper Cursor implementation inside a Stored Procedure.---

提交回复
热议问题