存储过程不会创建新表

时间:2016-11-24 21:55:56

标签: mysql sql

该过程假设采用现有表(oldtable)并创建新表(new_table_n)。它正在运行,但没有创建新表。任何帮助将不胜感激。

    DELIMITER $$
    CREATE PROCEDURE P_DYN_TABLE(IN OLDTABLE VARCHAR(30), IN NEW_TABLE_N VARCHAR(30))
    LANGUAGE SQL
    BEGIN

    DECLARE STATEMENT_1 VARCHAR(1000);

    DECLARE CONTINUE HANDLER FOR SQLSTATE '42704'
          SET STATEMENT_1 = '';
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION

    SET STATEMENT_1 = CONCAT('CREATE TABLE ',NEW_TABLE_N,' AS SELECT * FROM ',OLDTABLE,' WHERE 1=2');
    PREPARE S1 FROM @STATEMENT_1;
    EXECUTE S1;

    SET STATEMENT_1 = CONCAT('INSERT INTO ',NEW_TABLE_N,' SELECT * FROM ',OLDTABLE);
    PREPARE S2 FROM @STATEMENT_1;
    EXECUTE S2; 

    END; $$

1 个答案:

答案 0 :(得分:0)

DELIMITER $$
CREATE PROCEDURE P_DYN_TABLE(IN OLDTABLE VARCHAR(30), IN NEW_TABLE_N VARCHAR(30))
BEGIN
DECLARE STATMENT VARCHAR(1000);
SET @STATMENT="";
SET @STATMENT = CONCAT('CREATE TABLE ',NEW_TABLE_N,' AS SELECT * FROM ',OLDTABLE,' WHERE 1=2');
PREPARE S1 FROM @STATMENT;
EXECUTE S1;
DEALLOCATE PREPARE S1;
SET @STATMENT = CONCAT('INSERT INTO ',NEW_TABLE_N,' SELECT * FROM ',OLDTABLE);
PREPARE S1 FROM @STATMENT;
EXECUTE S1;
DEALLOCATE PREPARE S1;
END; $$