Solved

How to create a parametrized MySQL table?

Posted on 2014-09-19
2
209 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 32

Accepted Solution

by:
Stefan Hoffmann earned 500 total points
Comment Utility
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
Comment Utility
Fantastic!

By using this approach I solved all the issues

Drop table
create table
inserts....

Thank you very much!
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
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 …

772 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

11 Experts available now in Live!

Get 1:1 Help Now