MySql Procedure not returning value - mysql

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

Related

nesting of two cursor in mysql

i am trying to nest two cursors.however i am getting syntax for line "declare no_more rows boolean:=FALSE".i am using phpMyAdmin and running in this on MySQL console. Also can i do this without declaring stored procedure? please help me out here. this thing is breaking my head past three days. following is the best code i could write:
Delimiter $$
create procedure test1()
BEGIN
BLOCK1: begin
declare v_col1 int(10);
declare no_more_rows boolean1 := FALSE;
declare cursor1 cursor for
select content_id
from topic_list where topic_id=1;
declare continue handler for not found
set no_more_rows1 := TRUE;
open cursor1;
LOOP1: loop
fetch cursor1
into v_col1;
if no_more_rows1 then
close cursor1;
leave LOOP1;
end if;
BLOCK2: begin
declare v_col2 int(10);
declare no_more_rows2 boolean := FALSE;
declare cursor2 cursor for
select content_id
from content_upvotes
where u_id_upvoter = v_col1;
declare continue handler for not found
set no_more_rows2 := TRUE;
open cursor2;
LOOP2: loop
fetch cursor2
into v_col2;
if no_more_rows then
close cursor2;
leave LOOP2;
end if;
end loop LOOP2;
end BLOCK2;
end loop LOOP1;
end BLOCK1;
end $$
DELIMITER ;
If you check mysql's documentation on declare syntax, xou can see that the defsult value is set using the DEFAULT keyword, not the asdignment operator:
DECLARE var_name [, var_name] ... type [DEFAULT value]
So, your declaration should be
declare no_more_rows tinyint(1) default 0;
I also mapped the boolean type to tinyint(1).

How to set a variable into a loop in MySQL?

According to this answer Call a stored procedure for each row returned by a query in MySQL described as following:
CREATE PROCEDURE foo() BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE _id BIGINT UNSIGNED;
DECLARE cur CURSOR FOR SELECT id FROM objects WHERE ...;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
OPEN cur;
testLoop: LOOP
FETCH cur INTO _id;
IF done THEN
LEAVE testLoop;
END IF;
CALL testProc(_id);
END LOOP testLoop;
CLOSE cur;
END
How can i set a variable and increment it over each iteration?
The following code does't work and I don't know why.
CREATE PROCEDURE foo() BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE _id BIGINT UNSIGNED;
DECLARE counter INT DEFAULT 0;
DECLARE cur CURSOR FOR SELECT id FROM objects WHERE ...;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
OPEN cur;
testLoop: LOOP
FETCH cur INTO _id;
IF done THEN
LEAVE testLoop;
END IF;
CALL testProc(_id);
counter = counter + 1;
END LOOP testLoop;
CLOSE cur;
END

Stored procedure in mysql doesnt insert fetched and iterated values into table

This is what i tried. I couldnt get my values inserted into table. can anyone help me out ..
DELIMITER $$
USE `SampleDB`$$
DROP PROCEDURE IF EXISTS `Sample`$$
CREATE PROCEDURE `SampleDB`.`Sample`()
BEGIN
#declare variable
DECLARE tenantName VARCHAR(255);
DECLARE tenantAddress VARCHAR(255);
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR SELECT tenant_name,tenant_address FROM tenant;
#open cursor
OPEN cur1;
#starts the loop
the_loop: LOOP
#get the values of each column into our variables
FETCH cur1 INTO tenantName,tenantAddress;
IF done THEN
LEAVE the_loop;
END IF;
#Insert it
INSERT INTO tenant(tenant_name,tenant_address)
VALUES (tenantName,tenantAddress);
END LOOP the_loop;
CLOSE cur1;
END$$
DELIMITER ;
Indeed lack 13.6.7.2. DECLARE ... HANDLER Syntax:
...
DECLARE cur1 CURSOR FOR SELECT tenant_name, tenant_address FROM tenant;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
#open cursor
...
But do you really need a cursor? With a statement like this, you can avoid the cursor:
INSERT INTO `tenant` (`tenant_name`, `tenant_address`)
SELECT `tenant_name`, `tenant_address` FROM `tenant`;

Using for loop in MySql data set

How to use for loop in MySQL data set ?
FOR x IN (SELECT column FROM table_name WHERE column_2 = input_val)
LOOP
sum_total := sum_total + x.column ;
END LOOP;
This is an example how I used to loop data set in oracle.
How can this be achieved in MySql ?
How about not looping at all. It's SQL after all.
SELECT SUM(`column`) total
FROM table_name
WHERE column_2 = <input_val>
Otherwise use CURSOR
Now, equivalent loop using CURSOR will look like
DELIMITER $$
CREATE PROCEDURE sp_loop(IN input_val INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE sum_current, sum_total INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT column1 FROM table_name WHERE column2 = input_val;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO sum_current;
IF done THEN
LEAVE read_loop;
END IF;
SET sum_total = sum_total + sum_current;
END LOOP;
CLOSE cur1;
SELECT sum_total;
END$$
DELIMITER ;
Here is SQLFiddle
DROP PROCEDURE IF EXISTS `foo`.`usp_cursor_example`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `foo`.`usp_cursor_example`(
IN name_in VARCHAR(255)
)
READS SQL DATA
BEGIN
DECLARE name_val VARCHAR(255);
DECLARE status_update_val VARCHAR(255);
-- Declare variables used just for cursor and loop control
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
-- Declare the cursor
DECLARE friends_cur CURSOR FOR
SELECT
name
, status_update
FROM foo.friend_status
WHERE name = name_in;
-- Declare 'handlers' for exceptions
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
OPEN friends_cur;
select FOUND_ROWS() into num_rows;
the_loop: LOOP
FETCH friends_cur
INTO name_val
, status_update_val;
IF no_more_rows THEN
CLOSE friends_cur;
LEAVE the_loop;
END IF;
select name_val, status_update_val;
-- count the number of times looped
SET loop_cntr = loop_cntr + 1;
END LOOP the_loop;
-- 'print' the output so we can see they are the same
select num_rows, loop_cntr;
END
DELIMITER ;

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