1

The following code was used for finding the first five highest paid employee from a table named employee7 and add the details to another table named temp using cursor in mysql but I got the error

you have an error in your mysql syntax check the mannual that corresponds to mysql server version for the right syntax to use near 'loop cursorloop; close c7; end; at line 17

DELIMITER //
create procedure cursordemo()
begin 
    declare eno int(8);
    declare ename varchar(15);
    declare esal int(10);
    declare c7 cursor for select empno,empnm,empsal from employee7 order by empsal desc;
    open c7;
    cursorloop:loop
    fetch c7 into eno,ename,esal;
    if c7.rowcount>5 then  
        leave cursorloop;
    insert into temp values (esal,eno,ename);
    end loop cursorloop;
    close c7;
    end //
delimiter ;
5
  • You don't a CURSOR for this not even an stored procedure you can replace all with a simple query insert into temp values ([columns]) select [columns] from employee7 order by empsal desc Commented Feb 17, 2018 at 10:52
  • But what ever code I used for cursor showed the same error. Commented Feb 17, 2018 at 10:56
  • You are missing an end if; Commented Feb 17, 2018 at 11:00
  • Thank you it was the error ,but after running the program using the command select * from temp shows empty set as output eventhough the table employee7 have 8 rows of data. Commented Feb 17, 2018 at 11:19
  • you have mentioned the condition of rowcount>5 then leave the loop,then how come data will be inserted in the temp table? Commented Feb 17, 2018 at 12:55

1 Answer 1

5

I don't know where you got the idea of using c7.rowcount but that's not valid mysql the more usual way to exit a cursor loop in mysql is to use a handler fro example

DELIMITER //
create procedure cursordemo()
begin 
    declare eno int(8);
    declare ename varchar(15);
    declare esal int(10);
    declare done int default 0;
    declare c7 cursor for select emp_no,last_name,salary from employees order by salary desc;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    open c7;
    cursorloop:loop
    if done = true then  
        leave cursorloop;
    end if;
    fetch c7 into eno,ename,esal;

    insert into temp values (esal,eno,ename);
    end loop cursorloop;
    close c7;
    end //
delimiter ;
Sign up to request clarification or add additional context in comments.

4 Comments

But I want to select only the first five rows.
Either add a limit clause to your select statement or declare a counter , increment it by 1 each time thru the loop and add a test to the if condition.
In sql we use cursor%rowcount command is there any similar commands in mysql
Not that I know of. I assume you are used to plsql which is very different to mysql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.