mysql procedure error - mysql

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;

Related

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";

What is the right method to reuse a cursor inside a mysql stored procedured?

What is the right method to reuse a cursor inside a mysql stored procedure?
We're doing it like below, and It just does not look correct. Does anyone know whats the right method to do this?
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `fred`$$
CREATE DEFINER=`root`#`windows7.home` PROCEDURE `fred`()
BEGIN
DECLARE X INT;
DECLARE done INT DEFAULT FALSE;
DECLARE myType INT;
DECLARE cur1 CURSOR FOR SELECT val FROM checkval;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
SET X = 10;
WHILE X >= 2 DO
##############
OPEN cur1;
FETCH cur1 INTO myType;
read_loop: LOOP
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO myType VALUES (myType);
FETCH cur1 INTO myType;
END LOOP read_loop;
CLOSE cur1;
SET X = X-1;
SET done=FALSE;
##################
END WHILE;
END$$
DELIMITER ;
Heres the question that started it:
While loop in stored procedure loops only twice instead of 8 times

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 cursor: unknown column "done" in 'field list'

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;

stored procedure with cursor in MySQL

Can I use Cursor with stored procedure in MySQL? And can I receive the output values from it?
Yes;
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
OPEN cur2;
REPEAT
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF NOT done THEN
IF b < c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END IF;
UNTIL done END REPEAT;
CLOSE cur1;
CLOSE cur2;
END
Example taken from the manual.