within an MySQL stored procedure how can I dynamically set the variable name?

i want to iterate through the records in a table and place the values into variables within a stored procedure.

  DECLARE movetonext_tcl1 INT;
  DECLARE movetonext_tcl2 INT;
  DECLARE movetonext_tcl3 INT;
  DECLARE movetonext_tcl4 INT;
  DECLARE calculate_tcl1 INT;
  DECLARE calculate_tcl2 INT;
  DECLARE calculate_tcl3 INT;
  DECLARE calculate_tcl4 INT;
 
  declare x int;
 
  SET x = 1;
 
 
      WHILE x  <= 4 DO
             select movenextvalue FROM `tcl rules` where id=x into movetonext_tcl1;
             select calculatevalue FROM `tcl rules` where id=x into calculate_tcl1;
      SET  x = x + 1;
      END WHILE;

I need to know if it is possible to dynamically change the movetonext_tcl1 to tcl2,3 and 4 as I look through the statement.
NemetonaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
Just re-read the question and see you are trying to change the variable name you are passing to.
May be able to do this with dynamic code, but will post back with some thoughts on a different way.
Kevin CrossChief Technology OfficerCommented:
Does something like this work for you?

SELECT MAX(CASE id WHEN 1 THEN movenextvalue END)
     , MAX(CASE id WHEN 1 THEN calculatevalue END)
     , MAX(CASE id WHEN 2 THEN movenextvalue END)
     , MAX(CASE id WHEN 2 THEN calculatevalue END)
     , MAX(CASE id WHEN 3 THEN movenextvalue END)
     , MAX(CASE id WHEN 3 THEN calculatevalue END)
     , MAX(CASE id WHEN 4 THEN movenextvalue END)
     , MAX(CASE id WHEN 4 THEN calculatevalue END)
FROM `tcl rules` 
WHERE id <= 4
INTO movetonext_tcl1, calculate_tcl1
   , movetonext_tcl2, calculate_tcl2
   , movetonext_tcl3, calculate_tcl3
   , movetonext_tcl4, calculate_tcl4
;

Open in new window


It would let you set all 8 variables without a loop.
If not as the real-life scenario is more complex, you can read more on dynamic (prepared statements) approach here: http://dev.mysql.com/doc/refman/5.7/en/sql-syntax-prepared-statements.html

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NemetonaAuthor Commented:
Thank you, that works for me.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.