Jump to content


Photo

summing using dynamic sql

mysql

Best Answer requinix, 16 August 2017 - 08:46 PM

When it doubt, RTFM.

Because local variables are in scope only during stored program execution, references to them are not permitted in prepared statements created within a stored program. Prepared statement scope is the current session, not the stored program, so the statement could be executed after the program ends, at which point the variables would no longer be in scope. For example, SELECT ... INTO local_var cannot be used as a prepared statement. This restriction also applies to stored procedure and function parameters.

Go to the full post


  • Please log in to reply
5 replies to this topic

#1 shaddf

shaddf
  • Members
  • PipPipPip
  • Advanced Member
  • 102 posts

Posted 16 August 2017 - 07:26 AM

i have this table:

MatchID | PlayerID | Goals
--------------------------
 2         1          4
 2         3          3
 2         13         0
 2         9          0
 2         5          0

iam trying to get total  using dynamic sql query like so:
 

DECLARE TotalVariable INT;
SET @l_sql_total=CONCAT(' SELECT SUM(x.Goals) into ',TotalVariable,' FROM a');

but iam getting an error:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1

And when I change to this:

SET @l_sql_total=CONCAT(' SELECT SUM(x.Goals) into TotalVariable FROM a');

i get this error despite the fact that it has already been declared above:

ERROR 1327 (42000): Undeclared variable: TotalVariable

how can I clear this?



#2 requinix

requinix
  • Administrators
  • Maddening Administrator
  • 9,561 posts
  • LocationWA

Posted 16 August 2017 - 08:18 AM

DECLARE TotalVariable INT;
SELECT SUM(x.Goals) into TotalVariable FROM a;

The Reimann Zeta Function Trolley Problem | "Summer is when I, the great ice fairy, can show my true power!"

#3 shaddf

shaddf
  • Members
  • PipPipPip
  • Advanced Member
  • 102 posts

Posted 16 August 2017 - 11:54 AM

DECLARE TotalVariable INT;
SELECT SUM(x.Goals) into TotalVariable FROM a;

Iam using prepared statements to do this. And when i prepare that answer of yours ,it does not work



#4 requinix

requinix
  • Administrators
  • Maddening Administrator
  • 9,561 posts
  • LocationWA

Posted 16 August 2017 - 12:33 PM

You cannot SELECT...INTO a variable with a prepared statement.

What you've posted does not need to be prepared. What is your real procedure code?
The Reimann Zeta Function Trolley Problem | "Summer is when I, the great ice fairy, can show my true power!"

#5 shaddf

shaddf
  • Members
  • PipPipPip
  • Advanced Member
  • 102 posts

Posted 16 August 2017 - 07:10 PM

You cannot SELECT...INTO a variable with a prepared statement.

What you've posted does not need to be prepared. What is your real procedure code?

why not can you explain more please?



#6 requinix

requinix
  • Administrators
  • Maddening Administrator
  • 9,561 posts
  • LocationWA

Posted 16 August 2017 - 08:46 PM   Best Answer

When it doubt, RTFM.

Because local variables are in scope only during stored program execution, references to them are not permitted in prepared statements created within a stored program. Prepared statement scope is the current session, not the stored program, so the statement could be executed after the program ends, at which point the variables would no longer be in scope. For example, SELECT ... INTO local_var cannot be used as a prepared statement. This restriction also applies to stored procedure and function parameters.


The Reimann Zeta Function Trolley Problem | "Summer is when I, the great ice fairy, can show my true power!"




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users