Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 631111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:55:45+00:00 2026-05-13T19:55:45+00:00

I have a problem fetching values from a MySQL cursor. I create a temporary

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:55:45+00:00Added an answer on May 13, 2026 at 7:55 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 365k
  • Answers 365k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the data in the /proc filesystem to… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer Do you still get the error when you use a… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer You can use array_chunk to create a single array comprised… May 14, 2026 at 3:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.