How to create a parametrized MySQL table?

Posted on 2014-09-19
Medium Priority
Last Modified: 2014-09-19
Hi Expets!

Do you know a way to create a table with a name received as a parameter, inside a stored procedure?

by example:

CALL sp_pesquisa_lancamentos ("20140901","20140919",4,"the_name_I_want_for_the_table_to_be_created");

PROCEDURE sp_pesquisa_lancamentos (IN dt_ini date, IN dt_fim date, IN s_emp integer(11), IN  tabela varchar(20))

 CREATE TABLE  [tabela](
    Id int(6) NULL,
    CR varchar(50) NULL,
    DSP varchar(50) NULL,
    CT varchar(50) NULL,
    DCN varchar(10) NULL,
    DIA varchar(12) NULL,
    DT_LAN varchar(12) NULL,
    VALOR varchar(15) NULL,
    QTD varchar(5) NULL,
    DESTINO varchar(100) NULL,
    SALDO_C varchar(15) NULL,
    EMP varchar(10) NULL,
    ORIGEM  varchar(10) NULL


Open in new window

Thanks in advance!
Question by:Eduardo Fuerte
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 35

Accepted Solution

ste5an earned 2000 total points
ID: 40332585
You need dynamic SQL with PREPARE and EXECUTE. Concat the table name into the prepare statement. A table name as parameter is not supported.


PROCEDURE sp_pesquisa_lancamentos (
	IN dt_ini DATE, 
	IN dt_fim DATE, 
	IN s_emp INTEGER(11), 
	IN  tabela VARCHAR(20)

	SET @Sql = CONCAT( "DROP TABLE IF EXISTS ", tabela, ";");
	PREPARE preparedStmt FROM @Sql;
	EXECUTE preparedStmt;


Open in new window

Caveat: Cause you need string concatenation, this is prone to SQL injection. Consider a different approach, when posssible. Look for multitenant  solutions. E.g. use a discriminator in one big table instead of separate tables.

Author Closing Comment

by:Eduardo Fuerte
ID: 40333667

By using this approach I solved all the issues

Drop table
create table

Thank you very much!

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question