lior
lior

Reputation: 1177

Mysql store procedure get table name as parameter

I have a mysql procedure that inserts a row to a table. Is it possible to pass the name of the table as a parameter?

I tried and got an unknown table exception.

EDIT:

my procedure is called from insaid a trriger:

CREATE  PROCEDURE `blabla`(tableName VARCHAR(255), var1 VARCHAR(255), var2 VARCHAR(255), var3 VARCHAR(255))
BEGIN
            INSERT INTO tableName 
            SET
                var1 = var1,
                var2= var2,
                var3= var3;

END

the error: ERROR 1146: 1146: Table 'xxx_xx_admin.tableName' doesn't exist

Upvotes: 1

Views: 1223

Answers (1)

davek
davek

Reputation: 22895

It is possible, but only if you use dynamic SQL to build your SQL. MySQL implements this using PREPARE and EXECUTE.

See How To have Dynamic SQL in MySQL Stored Procedure and http://rpbouman.blogspot.de/2005/11/mysql-5-prepared-statement-syntax-and.html

Upvotes: 1

Related Questions