MySQL cursor: unknown column "done" in 'field list' - mysql

I'm trying to use a cursor loop in MYSQL, but it's not working. I've essentially copied the example from http://dev.mysql.com/doc/refman/5.0/en/cursors.html, except I use a function instead of a procedure. Does that matter?
When I try to run the function - select xxx() from dual - I get an error: unknown column "done" in 'field list'. What to do?
delimiter $$
create function xxx()
returns int deterministic
begin
DECLARE a INT;
DECLARE cur1 CURSOR FOR SELECT id FROM my_table;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a;
IF done THEN
LEAVE read_loop;
END IF;
END LOOP;
CLOSE cur1;
return 1;
end$$
delimiter ;

You didnt declared done.
DECLARE done INT DEFAULT FALSE;
and at the end
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

Related

Cursor incorrect integer value error

I am trying to make my first cursor in MySQL and I am receiving an error. It says incorrect integer value. I was thinking this would grab the value in row one from column customer_Id, and store it into the IdValue variable. How do I code this correctly and fix this error?
DELIMITER $$
CREATE PROCEDURE CursorProcedure()
BEGIN
DECLARE IdValue int;
DECLARE myCursor CURSOR FOR
SELECT customer_Id FROM customers;
OPEN myCursor;
FETCH myCursor INTO IdValue;
CLOSE myCursor;
SELECT IdValue;
END$$
DELIMITER ;
DROP PROCEDURE IF EXISTS CursorProcedure;
DELIMITER $$
CREATE PROCEDURE CursorProcedure()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE IdValue int;
DECLARE myCursor CURSOR FOR SELECT customer_Id FROM customers;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN myCursor;
read_loop: LOOP
FETCH myCursor INTO IdValue;
IF done THEN
LEAVE read_loop;
END IF;
--
-- YOU ARE IN YOUR READ LOOP
-- DO SOMETHING WITH IT HERE
--
END LOOP;
CLOSE myCursor;
END$$
DELIMITER ;
But in the "do something with it here", don't do a SELECT on it because it will generate multiple result sets. Do something meaningful.
Manual page on Cursors.
As an aside, cursors are rarely your friends. They are extremely slow. Use them in dire emergencies only.

using cursor last returned row is repeated

I have a table named as TRY like this....
try(name,sal) values('tony',10000),('david',20000),('rony',30000),('sami',40000)
Now I'm using cursor in a procedure to show all the values of the salary column through a variable.Doing this I'm trying to make out that hoe a cursor works as I'm new to cursor and I have come to know that cursor fetches every selected row one by one.I'm doing this following code...
delimiter ;;
create procedure me()
begin
declare done int default 0;
declare var int;
declare cur cursor for select sal from try;
declare continue handler for not found set done=1;
open cur;
curloop:loop
if done=1 then
leave curloop;
end if;
fetch cur into var;
select var;
end loop;
close cur;
end;;
Using this I'm getting all the values of SAL column correctly but the problem is that it's returning an extra row which is duplicate of the last value of SAL column,i.e. I'm getting the last value repeated.
Please solve my problem.Thanks in advance.
You need to change the function a little, check done just after FETCH operation -
CREATE PROCEDURE me()
BEGIN
DECLARE done int DEFAULT 0;
DECLARE var int;
DECLARE cur CURSOR FOR SELECT sal FROM try;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
curloop:
LOOP
FETCH cur INTO var;
IF done THEN
LEAVE curloop;
END IF;
SELECT var;
END LOOP;
CLOSE cur;
END

MySql Procedure not returning value

I'm unable to get the result of OUT variable in this code, am I doing wrong something? can anyone help?
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE cid INT(10);
DECLARE cuserid VARCHAR(50);
DECLARE cur1 CURSOR FOR SELECT id,username FROM tblcustomer;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO cid,cuserid;
IF done THEN
LEAVE read_loop;
END IF;
SET customers_list = CONCAT(customers_list,cid,':',cuserid,',');
END LOOP;
CLOSE cur1;
END
The procedure prototype is missing from your snippet, but assuming the below:
CREATE PROCEDURE foo(OUT customers_list VARCHAR(100))
BEGIN
...
SET customers_list = 'foo-list';
END ;
This is how you would retreive your "return value":
CALL foo(#var);
SELECT #var; -- outputs "foo-list"
Strictly speaking, a procedure has no "return value". A function has:
CREATE FUNCTION bar() RETURNS VARCHAR(100)
BEGIN
...
RETURN 'bar-list';
END ;
SELECT bar(); -- outputs "bar-list";

Printing out multiple rows in stored procedure

I want to pass in an int value and get back a list of short passwords that are less than this int. I have had trouble modifying the MySQL cursors example: http://dev.mysql.com/doc/refman/5.0/en/cursors.html. I want to print out the rows with short passwords but any time the result has more than one row PhpMyAdmin just displays "Processing request" indefinitely. What am I doing wrong? Its ruining my Saturday productivity!
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `ShortPasswd`(IN `passwordLength` TINYINT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a INT;
DECLARE b VARCHAR(255);
DECLARE cur1 CURSOR FOR SELECT UID, Password
FROM authentication WHERE CHARACTER_length(Password) < passwordLength;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a, b;
IF done THEN
LEAVE read_loop;
END IF;
SELECT b;
END LOOP;
CLOSE cur1;
END
$$
DELIMITER ;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `ShortPasswd`(IN `passwordLength` TINYINT)
BEGIN
SELECT UID, Password
FROM authentication WHERE CHARACTER_length(Password) > passwordLength;
END
$$
DELIMITER ;
You will get rows UID, password directly. as you get from a query when you
call ShortPasswd;
I suggest to modify these lines:
...
DECLARE FINISHED BOOLEAN DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
...
IF done THEN
CLOSE cur1;
LEAVE read_loop;
END IF;
...
END LOOP read_loop;
I use the 0/1 instead of FALSE/TRUE, and close the cursor before leave the LOOP.

mysql procedure error

DELIMITER $$
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE b, c INT;
DECLARE cur1 CURSOR FOR SELECT empid FROM test.mytable;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO b;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO test.test_log VALUES (b,'test');
CLOSE cur1;
END $$
When compiling this code I an getting 1064 error can some one pls help me to find the error
I think this will help u. Put END LOOP; before CLOSE cur1;