Re: Stored procedure - variabile per creazione tabella
stefano fulgenzi Wrote:
-------------------------------------------------------
> 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
> LT 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)
Ciao Stefano
grazie mille per il tuo aiuto. Funziona tutto alla perfezione!!!
Approfitto della tua conoscenza chiedendoti se รจ preferibile popolare la tabella appena creata in una SP separata, o all'interno della stessa.
Questa SP mi serve prettamente per alleggerire una tabella, spostando tutti i log per ogni mese che termina
Grazie ancora