MySQL验证所有列,如果不存在则添加

时间:2014-02-20 18:12:20

标签: php mysql

我尝试检查MySql ddb中是否存在所有列。我使用这个答案MySQL add column if not exist,但我有1064错误。 Anyboby可以帮助我吗?

由于

    foreach($source_bdd as $each_column)
    {
        $source_column = explode('***',$each_column);
        $nullable = $source_column[4] == 'YES' ? 'DEFAULT NULL' : 'NOT NULL';
        $verify_and_update_column .= '
            DELIMITER $$
            DROP PROCEDURE IF EXISTS Alter_Table_'.$source_column[0].''.$source_column[1].' $$
            CREATE PROCEDURE Alter_Table_'.$source_column[0].''.$source_column[1].'()
            BEGIN
            DECLARE '.$source_column[0].''.$source_column[1].'_count INT;
            SET '.$source_column[0].''.$source_column[1].'_count = ( SELECT COUNT(*) 
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = "'.$source_column[0].'" AND 
            COLUMN_NAME = "'.$source_column[1].'");
            IF '.$source_column[0].''.$source_column[1].'_count = 0 THEN
            ALTER TABLE '.$source_column[0].'
            ADD COLUMN '.$source_column[1].' '.$source_column[9].' '.$nullable.';
            END IF;
            END $$ 
            CALL Alter_Table_'.$source_column[0].''.$source_column[1].'() $$
            DELIMITER ;
        ';
    }

以下是查询的摘录和最后的错误:

            DELIMITER $$
            DROP PROCEDURE IF EXISTS Alter_Table_table_1_column_1 $$
            CREATE PROCEDURE Alter_Table_table_1_column_1()
            BEGIN
            DECLARE table_1_column_1_count INT;
            SET table_1_column_1_count = ( SELECT COUNT(*) 
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = "table_1" AND 
            COLUMN_NAME = "table_1");
            IF table_1_column_1_count = 0 THEN
            ALTER TABLE table_1
            ADD COLUMN table_1 varchar(512) DEFAULT NULL;
            END IF;
            END $$ 
            CALL Alter_Table_table_1_column_1() $$
            DELIMITER ;


            DELIMITER $$
            DROP PROCEDURE IF EXISTS Alter_Table_table_1_column_2 $$
            CREATE PROCEDURE Alter_Table_table_1_column_2()
            BEGIN
            DECLARE table_1_column_2_count INT;
            SET table_1_column_2_count = ( SELECT COUNT(*) 
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = "table_1" AND 
            COLUMN_NAME = "table_1");
            IF table_1_column_2_count = 0 THEN
            ALTER TABLE table_1
            ADD COLUMN table_1 varchar(512) DEFAULT NULL;
            END IF;
            END $$ 
            CALL Alter_Table_table_1_column_2() $$
            DELIMITER ;

...

            DELIMITER $$
            DROP PROCEDURE IF EXISTS Alter_Table_table_25_column_12 $$
            CREATE PROCEDURE Alter_Table_table_25_column_12()
            BEGIN
            DECLARE table_25_column_12_count INT;
            SET table_25_column_12_count = ( SELECT COUNT(*) 
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = "table_1" AND 
            COLUMN_NAME = "table_1");
            IF table_25_column_12_count = 0 THEN
            ALTER TABLE table_1
            ADD COLUMN table_1 varchar(512) DEFAULT NULL;
            END IF;
            END $$ 
            CALL Alter_Table_table_25_column_12() $$
            DELIMITER ;

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'DELIMITER $$附近使用正确的语法 DROP程序如果存在Alter_Table_table_1_column_1 $$ 在第11064行创建PROC'

0 个答案:

没有答案