Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use a variable for table name in mysql sproc

I'm trying to pass a table name into my mysql stored procedure to use this sproc to select off of different tables but it's not working...

this is what I"m trying:

CREATE PROCEDURE `usp_SelectFromTables`(  IN TableName varchar(100) ) BEGIN         SELECT * FROM @TableName; END 

I've also tried it w/o the @ sign and that just tells me that TableName doesn't exist...which I know :)

like image 790
Kyle Avatar asked May 02 '10 18:05

Kyle


People also ask

Does MySQL support table variable?

TO answer your question: no, MySQL does not support Table-typed variables in the same manner that SQL Server (http://msdn.microsoft.com/en-us/library/ms188927.aspx) provides.

Can I pass table variable to stored procedure?

You cannot pass table-valued parameters to CLR user-defined functions. Table-valued parameters can only be indexed to support UNIQUE or PRIMARY KEY constraints. SQL Server does not maintain statistics on table-valued parameters.

How do I create a variable in MySQL?

MySQL variable assignment There are two ways to assign a value to a user-defined variable. You can use either := or = as the assignment operator in the SET statement. For example, the statement assigns number 100 to the variable @counter. The second way to assign a value to a variable is to use the SELECT statement.


2 Answers

SET @cname:='jello'; SET @vname:='dwb'; SET @sql_text = concat('select concept_id,concept_name,',@vname,' from enc2.concept a JOIN enc2.ratings b USING(concept_id) where concept_name like (''%',@cname,'%'') and 3 is not null order by 3 asc');  PREPARE stmt FROM @sql_text; EXECUTE stmt; DEALLOCATE PREPARE stmt; 
like image 64
Angelo Avatar answered Oct 10 '22 12:10

Angelo


An extra bit that caused me problems.

I wanted to set the table name and field dynamically in a query as @kyle asked, but I also wanted to store the result of that query into a variable @a within the query.

Instead of putting the variable @a into the concat literally, you need to include it as part of the string text.

delimiter //  CREATE PROCEDURE removeProcessed(table_name VARCHAR(255), keyField VARCHAR(255), maxId INT, num_rows INT)  BEGIN   SET @table_name = table_name;   SET @keyField = keyField;   SET @maxId = maxId;   SET @num_rows = num_rows;    SET @sql_text1 = concat('SELECT MIN(',@keyField,') INTO @a FROM ',@table_name);   PREPARE stmt1 FROM @sql_text1;   EXECUTE stmt1;   DEALLOCATE PREPARE stmt1;    loop_label:  LOOP     SET @sql_text2 = concat('SELECT ',@keyField,' INTO @z FROM ',@table_name,' WHERE ',@keyField,' >= ',@a,' ORDER BY ',@keyField,' LIMIT ',@num_rows,',1');     PREPARE stmt2 FROM @sql_text2;     EXECUTE stmt2;     DEALLOCATE PREPARE stmt2;      ...Additional looping code...      END LOOP; END //  delimiter ; 

So in @sql_text1 assign the result of the query to @a within the string using:

') INTO @a FROM '

Then in @sql_text2 use @a as an actual variable:

,' WHERE ',@keyField,' >= ',@a,' ORDER BY '

like image 20
codewaggle Avatar answered Oct 10 '22 12:10

codewaggle