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

  • Home
  • SEARCH
  • 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 3303006
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:54:43+00:00 2026-05-17T20:54:43+00:00

I want the procedure to take parameter answertable and partid in the select statement,

  • 0

I want the procedure to take parameter answertable and partid in the select statement,
but when i call it it doesn’t replace the parameter answertable with the value

the call call updateTotalScores('quiz_participation', 'quiz_answer', 1)

returns the error: 1146 - Table 'quizdb.answertable' doesn't exist

passing the id works, but passing the table name doesn’t
so how do i pass the table name to the select in

DECLARE cur1 CURSOR FOR SELECT SUM(`score`), SUM(`maxscore`) FROM answertable WHERE `idParticipation`=partid;

entire procedure:

DELIMITER $$
CREATE PROCEDURE updateTotalScores(IN participationtable CHAR(64), IN answertable CHAR(64), IN partid INT)
BEGIN
 DECLARE done INTEGER DEFAULT 0;
 DECLARE sscore INTEGER DEFAULT 0;
 DECLARE smaxscore INTEGER DEFAULT 0;
 DECLARE cur1 CURSOR FOR SELECT SUM(`score`), SUM(`maxscore`) FROM answertable WHERE `idParticipation`=partid;
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

 OPEN cur1;
 REPEAT
  FETCH cur1 INTO sscore, smaxscore;
  UNTIL done = 1
 END REPEAT;
 CLOSE cur1;

 UPDATE participationtable SET `score`=sscore, `maxscore`=smaxscore WHERE `idParticipation`=partid;
END $$
DELIMITER ;

For completeness

the table name cannot be passed to a MySql cursor, at least not yet

http://forge.mysql.com/worklog/task.php?id=3433

the answer from below (corrected a bit)

DELIMITER $$

CREATE PROCEDURE updateTotalScores(IN participation_table VARCHAR(45), IN answer_table VARCHAR(45), IN part_id INT)
BEGIN
    SET @stmt_text=CONCAT("SELECT @score := SUM(`score`), @maxscore := SUM(`maxscore`) FROM ",
                         answer_table, " WHERE `idParticipation`=",  part_id);
    PREPARE stmt FROM @stmt_text;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

    SET @stmt_text=CONCAT("UPDATE ", participation_table, 
                        " SET `score`=?, `maxscore`=? WHERE `idParticipation`=", part_id);
    PREPARE stmt FROM @stmt_text;
    EXECUTE stmt USING @score, @maxscore;
    DEALLOCATE PREPARE stmt;
END $$
  • 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-17T20:54:44+00:00Added an answer on May 17, 2026 at 8:54 pm

    I believe you cannot do it in this manner.

    In order to achieve this, you should use Dynamic SQL.

    Note that you cannot open a cursor using Dynamic SQL either. But in your case, there seems to be no need for a cursor.

    If i understand your code correctly, you can just use user variables and probably achieve what you are trying to do using 2 Dynamically prepared statements.

      SET @stmt_text=CONCAT("SELECT @score = SUM(`score`), @maxscore=SUM(`maxscore`) FROM ",                
                             answertable, "WHERE `idParticipation`= ",  partid);
      PREPARE stmt FROM @stmt_text;
      EXECUTE stmt USING @a;
    

    And then you update the values using the below statement

      SET @stmt_text=CONCAT("UPDATE", participationtable, " SET `score`=@score,  
                          `maxscore`=@maxscore WHERE `idParticipation`=", partid);
    
      PREPARE stmt FROM @stmt_text;
      EXECUTE stmt USING @a;
    
      DEALLOCATE PREPARE stmt;
    

    Note: Please check the syntax. I cannot test it to verify it exactly but i hope you get the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is my stored procedure. I want use stored procedure select all row of
Scenario I have a stored procedure that takes a single parameter. I want to
I want to execute stored procedure with one parameter that returns table using EF4
I want to write a stored procedure that will accept a parameter of @searchString.
If you're creating a temporary table within a stored procedure and want to add
I want to analyze an SQL Server stored procedure from .NET code to retrieve
I want to generate some XML in a stored procedure based on data in
I want to dynamically get the name of the current stored procedure inside itself
I have a Stored Procedure that rolls-back a series of operations. I want to
I have a large CSV file and I want to execute a stored procedure

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.