MySQL Forums
Forum List  »  Italian

Re: Stored procedure - variabile per creazione tabella
Posted by: stefano fulgenzi
Date: November 12, 2018 06:08AM

Ciao,
per effettuare il passaggio di una variabile nella creazione del nome tabella (ma anche per altri statement), devi utilizzare l'istruzione di PREPARE.

---------------------
DROP PROCEDURE IF EXISTS `table_new`;

DELIMITER $$


CREATE DEFINER=`root`@`localhost` PROCEDURE `table_new`()
BEGIN
-- declare variables
DECLARE name_table varchar(15);

-- setting statement
SET @name_table=MONTHNAME(NOW());
SET @table = CONCAT('CREATE TABLE `',@name_table,'` (
`TimeLog` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Zona` varchar(22) NOT NULL,
`Sensore` varchar(20) NOT NULL,
`T` float NOT NULL,
`U` float NOT NULL,
KEY `TimeLog` (`TimeLog`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;');

PREPARE stmt from @table;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;


END
$$

---------------------

mysql> call table_new();
Query OK, 0 rows affected (0,01 sec)

mysql> show tables;
+-------------------+
| Tables_in_vulcano |
+-------------------+
| november |
+-------------------+
1 row in set (0,00 sec)

mysql> show create table november\G
*************************** 1. row ***************************
Table: november
Create Table: CREATE TABLE `november` (
`TimeLog` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`Zona` varchar(22) NOT NULL,
`Sensore` varchar(20) NOT NULL,
`T` float NOT NULL,
`U` float NOT NULL,
KEY `TimeLog` (`TimeLog`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0,02 sec)

System & Database Administrator MySQL
canale Telegram MySQL: https://t.me/mysqld_it

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: Stored procedure - variabile per creazione tabella
579
November 12, 2018 06:08AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.