Solved

How to create a parametrized MySQL table?

Posted on 2014-09-19
2
213 Views
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))
BEGIN
.....

 DROP TABLE IF EXISTS [tabela];
 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!
0
Comment
Question by:Eduardo Fuerte
2 Comments
 
LVL 33

Accepted Solution

by:
ste5an earned 500 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.

E.g.

PROCEDURE sp_pesquisa_lancamentos (
	IN dt_ini DATE, 
	IN dt_fim DATE, 
	IN s_emp INTEGER(11), 
	IN  tabela VARCHAR(20)
	)
BEGIN
	--[..]

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

	--[..]
END;

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.
0
 

Author Closing Comment

by:Eduardo Fuerte
ID: 40333667
Fantastic!

By using this approach I solved all the issues

Drop table
create table
inserts....

Thank you very much!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now