SQL Query with variable name table in Stored Procedure using MySql 8 version

Hi,

This is my SQL query that working correctly and it’s stored an a database MySql version 8.0.17

(I’m sorry but if I’m paste the query code the question is not posted… does anyone know why?)

enter image description here

I’ve tried using this SQL query on an a Stored Procedure on this mode

(I’m sorry but if I’m paste the stored procedure code the question is not posted… does anyone know why?)

enter image description here

But I’ve this error

Procedure execution failed 1146 - Table ‘db_test.’,tablename,’’ doesn’t exist Time: 0,138s

Any help would greatly appreciate… Thank you.

You are not going to be able to build a variable and then use that variable in the SQL for the tablename just be concatenating it like that. It appears there is a way using prepared statements. Check out the following stack overflow (second answer has an example that may work)

:slight_smile:

Thanks for reply.

I’ve tried this code without success

Immagine

I’ve this error

MySQL 1064 Error: 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 ‘",pname,’’,contents,NULL)) AS ', pname, '')

You have used a single quote that breaks your syntax here in the CONCAT function. Pleasev try this - CONCAT(“max(IF(‘pname’=”,pname,’’, contents,NULL).

1 Like

image

What is the value of @sql?

1 Like

Thanks all, resolved —

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.