MySQL Pass table name to cursor select

后端 未结 1 1040
难免孤独
难免孤独 2021-01-06 12:41

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

相关标签:
1条回答
  • 2021-01-06 13:28

    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 讨论(0)
提交回复
热议问题