views:

443

answers:

1

Hello,

I have a problem fetching values from a MySQL cursor.

I create a temporary table which is a mere copy of another table (the original table has a variable name, that's passed as the procedure's parameter, and because MySQL doesn't support variable table names, I have to create a copy - can't use the original directly).

The temporary table creation goes just fine, all data that are supposed to be in it are there. Then I define a cursor to iterate through my temporary table... but when I try to fetch from the cursor in a while loop my variables are not filled with data from the "cursored" table... most of them are just NULL, only last 2 seem to have correct values inside.

Here is the chunk of my code:

-- variables to be filled from the cursor
DECLARE id,rain,snow,hs,clouds,rain2,cape,status,done int;
DECLARE v_v,v_u double;

-- cursor declaration
DECLARE iter CURSOR FOR (SELECT id,cape,rain,snow,hstones,clouds,raining,wind_u,wind_v FROM temp_tbl);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

-- drop the old temporary table if any
DROP TABLE IF EXISTS temp_tbl;

-- a statement to create the temporary table from a table with the specified name
-- (table_name is a parameter of the stored procedure this chunk is from)

SET @temp_table = CONCAT('CREATE TABLE temp_tbl AS SELECT * FROM ', table_name, ' WHERE 1'); 

-- prepare, execute and deallocate the statement
PREPARE ctmp FROM @temp_table;
EXECUTE ctmp;
DEALLOCATE PREPARE ctmp;

-- now the temp_table exists, open the cursor
OPEN iter;

WHILE NOT done DO

        -- fetch the values
        FETCH iter INTO id,cape,rain,snow,hs,clouds,rain2,v_u,v_v;

        -- fetch doesnt work, only v_u and v_v variables are fetched correctly, others are null

        -- ... further statements go here

END WHILE;

CLOSE iter;

Is there any type-checking within the FETCH statement that might cause such problem? The columns in my temporary table (which is derived from the original one) are just small-ints or tiny-ints, so these should be perfectly compatible with ints I use in the fetch statement. Those two last ones are doubles, but weird that only these two doubles are fetched. Even the ID int column, which is the primary key isn't fetched.

I work with the dbForge Studio to step into and debug my procedures, but that shouldn't be the problem.

Thank you in advance for any advices or work-arounds.

+3  A: 

In MySQL functions, when the parameter or variable names conflict with the field names, the parameter or variable names are used.

In these statements:

DECLARE id,rain,snow,hs,clouds,rain2,cape,status,done int;

DECLARE iter CURSOR FOR (SELECT id,cape,rain,snow,hstones,clouds,raining,wind_u,wind_v FROM temp_tbl);

you select the uninitialized variables, not the fields. It is the same as doing:

DECLARE iter CURSOR FOR (SELECT NULL, NULL, NULL, NULL, NULL, NULL, NULL, wind_u,wind_v FROM temp_tbl);

The last two field name do not conflict and are selected correctly.

Prepend the variable names with an underscore:

DECLARE _id, _rain, _snow, _hs, _clouds, _rain2, _cape, _status, _done INT;
Quassnoi
Thanks a lot! Worked like a charm.
NumberFour
Awesome, I had the same problem. Thanks!
sohtimsso1970