Running this routine on HeidiSQL and it runs with 0 rows affected, even though there should be exactly 1 row affected. both select statements seem to work fine outside this Stored Procedure.
BEGIN
DECLARE someId INT;
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR
select anotherId from tableA
where yetAnotherId IN(another select statement);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
IF done THEN
LEAVE read_loop;
END IF;
FETCH cur1 INTO someId;
update tableB
set x = 'hello', y = 'world'
where something = someId;
END LOOP;
CLOSE cur1;
END;
I would like to get an idea what could be wrong with the structure of this routine. It looks to me that even thoughthe cursor should contain 1 entry, It does not.
Thanks
EDIT: It looks like 'someId' was matching a table field with the same name, thus the issue. This has been resolved now.
Your SP looks good, I have made changes for MySql please try it
BEGIN
DECLARE someId INT;
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR
select anotherId from tableA
where yetAnotherId IN(another select statement);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO someId;
update tableB
set x = 'hello', y = 'world'
where something = someId;
IF done THEN
CLOSE cur1;
LEAVE read_loop;
END IF;
END LOOP read_loop;
END;
Related
I have a stored procedure snippet like this
CREATE DEFINER=`root`#`%` PROCEDURE `new_procedure`()
BEGIN
DECLARE c_id varchar(100) DEFAULT "";
DECLARE cursor1 CURSOR for select id from t1 where name like 's%';
OPEN cursor1;
get_t1: LOOP
FETCH cursor1 into c_id;
p:begin
declare cursor2 cursor for select id from t2 where cl_id=c_id;
open cursor2;
get_t2: loop
fetch project_cursor into p_id;
// perform some tasks
t:begin
declare cursor3 cursor for select id from t3 where pr_id=p_id;
open cursor3;
get_t3:loop
fetch cursor3 into t_id;
IF v_finished = 1 THEN
LEAVE get_t3;
END IF;
//perform some tasks
close cursor3;
select p_id;
end t;
end loop t2;
end p;
END LOOP t1;
END
here select statement is not displaying any data
when i write select under cursor 3 it is working.
did i made any mistake? anything I missed
Some basic logic has been added in your SP, Try it whether this is what you are expecting.
CREATE DEFINER=`root`#`%` PROCEDURE `new_procedure`()
BEGIN
DECLARE c_id VARCHAR(100) DEFAULT "";
DECLARE done1 BOOLEAN DEFAULT FALSE;
DECLARE cursor1 CURSOR FOR SELECT id FROM t1 WHERE name LIKE 's%';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done1=TRUE;
OPEN cursor1;
GET_T1: LOOP
FETCH cursor1 INTO c_id;
IF done1 THEN
LEAVE GET_T1;
END IF;
P:BEGIN
DECLARE p_id INT ; -- check which data types suits you, might be int
DECLARE done2 BOOLEAN DEFAULT FALSE;
DECLARE cursor2 CURSOR FOR SELECT id FROM t2 WHERE cl_id=c_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2=TRUE;
OPEN cursor2;
GET_T2: LOOP
FETCH cursor2 INTO p_id;
IF done2 THEN
LEAVE GET_T2;
END IF;
/* perform some task */
T:BEGIN
DECLARE t_id INT ; -- check which data types suits you, might be int
DECLARE done3 BOOLEAN DEFAULT FALSE;
DECLARE cursor3 CURSOR FOR SELECT id FROM t3 WHERE pr_id=p_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done3=TRUE;
OPEN cursor3;
GET_T3:LOOP
FETCH cursor3 INTO t_id;
IF done3 THEN
LEAVE GET_T3;
END IF;
/* perform some tasks */
#select p_id; -- don't know what you are doing with this
END LOOP GET_T3;
CLOSE cursor3;
SET done3=FALSE;
END T;
END LOOP GET_T2;
CLOSE cursor2;
SET done2= FALSE;
END P;
END LOOP GET_T1;
CLOSE cursor1;
SET done1= FALSE;
END
I did a mysql procedure that insert in a table a number of rows for each row of other table. Thats the code:
PROCEDURE `name` (`var1` SMALLINT(6), `var2` SMALLINT(6)) BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a SMALLINT(6);
DECLARE cur CURSOR FOR SELECT id FROM tableA WHERE fk_id = var2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
IF done THEN
LEAVE read_loop;
END IF;
FETCH cur INTO a;
INSERT INTO tableB VALUES(var1, a);
END LOOP;
CLOSE cur;
My problem is that the last row is inserted two times. How can I resolve this?
Thanks
I am trying to use the below procedure to update a table but i could not compile the procedure.
CREATE
PROCEDURE `propmanage2016`.`test`()
DECLARE CURSOR cur1 FOR
SELECT unit_id, unit_code FROM t_units WHERE unit_projectid = 1;
DECLARE done INT DEFAULT FALSE;
DECLARE a INT;
DECLARE b VARCHAR(200);
DECLARE done INT DEFAULT FALSE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
BEGIN
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO b , a ;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE t_owner_resident SET or_unit = a WHERE unit_name = b;
END LOOP;
CLOSE cur1;
END;
please give some help
Sure you can't do it like this without the stored procedure?
UPDATE t_owner_resident ow INNER JOIN t_units t set ow.unit_name = t.unit_code
WHERE o.unit_name = t.unit_id and t.unit_projectid = 1
Note: I think a,b is mixed up in your SP, so please excuse if i've got the column names slightly mixed up too
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
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";