Search code examples
mysqlstored-procedurescursor

Stored proc using cursor for looping and storing return value into variable


My query should while looping through cursor for table table_a check if a value exists in table_b. If a value exists in table_b then return a value into the variable @yyy.
When I run this stored proc I should get a value that returns col2,col3,col1. but it returns only col2, col3.
In this query when I'm using the into @yyy I feel its not working the way it needs to. Not sure what the problem is. Can you please help.
Just by removing into @yyy I can kind of get right results but I needs to make more changes to the variable @yyy which is why I need to store the results into it.

Delimiter $$
DROP PROCEDURE IF EXISTS sp_test3;
CREATE PROCEDURE sp_test3()
BEGIN
DECLARE DONE INT DEFAULT 0;
DECLARE col1 varchar(255);
DECLARE curA CURSOR FOR SELECT  a1 FROM table_a;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE = 1;
OPEN curA;
SET @SQL_TXT = '';
while done = 0 do  
fetch next from CurA into col1;
if done = 0 then
SET @xxx = CONCAT("select b1 into @yyy  from table_b where b1 ='", 
col1,"'");
PREPARE stmt_name FROM @xxx;
EXECUTE stmt_name;
DEALLOCATE PREPARE stmt_name;
SELECT  @yyy;
END IF;
END WHILE;
close curA;
end
$$  

create table scripts below:

      create table table_a(a1 varchar(255));      
      create table table_b(b1 varchar(255));    

      insert into table_a values('col2');
      insert into table_a values('col3');
      insert into table_a values('col5');
      insert into table_a values('col1');

      insert into table_b values('col2');
      insert into table_b values('col3');
      insert into table_b values('col4');
      insert into table_b values('col1');

Solution

  • drop procedure if exists sp_test3;
    drop table if exists table_b, table_a;
    
    create table if not exists table_a(a1 varchar(255));
    create table if not exists table_b(b1 varchar(255));
    
    insert into table_a values ('col2');
    insert into table_a values ('col3');
    insert into table_a values ('col5');
    insert into table_a values ('col1');
    
    insert into table_b values ('col2');
    insert into table_b values ('col3');
    insert into table_b values ('col4');
    insert into table_b values ('col1');
    
     CREATE PROCEDURE sp_test3()
     BEGIN
     DECLARE DONE, DONE1 INT DEFAULT 0;
     DECLARE col1 varchar(255);
     DECLARE curA CURSOR FOR SELECT a1 FROM table_a;
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE = 1;
     OPEN curA;
     SET @SQL_TXT = '';
        while done = 0 do  
         fetch next from CurA into col1;
         if done = 0 then
         BEGIN
         DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE1 = 1;
         SET @xxx = CONCAT("select b1 into @yyy 
                           from table_b
                           where b1 = '", col1, "'");
         PREPARE stmt_name FROM @xxx;
         EXECUTE stmt_name;
         DEALLOCATE PREPARE stmt_name;
         if (DONE1 = 0) THEN
           SELECT @yyy;
         ELSE
           SET DONE1 = 0;
         END IF;
         END;
         END IF;
       END WHILE;
      close curA;
     end;