CREATE PROCEDURE sp_tables_if_change_123(
IN P_TABLE_NAME VARCHAR(100),
IN P_TABLE_SCHEMA VARCHAR(100),
IN P_BACKUP_DATABSAE VARCHAR(100)
)
BEGIN
DECLARE cnt int;
DECLARE i int DEFAULT 1;
DECLARE TABLE_i,COLUMN_i VARCHAR(100);
set @tsql = CONCAT(“SELECT count(o.COLUMN_NAME) into @cnt FROM ( SELECT a.COLUMN_NAME FROM information_schema.COLUMNS a WHERE “,
case when P_TABLE_NAME is not null then CONCAT(“a.TABLE_NAME = “””,P_TABLE_NAME,””””) else “” end,
case when P_TABLE_SCHEMA is not null then CONCAT(” AND a.TABLE_SCHEMA = “””,P_TABLE_SCHEMA,””””) ELSE “” end,
“) o WHERE o.COLUMN_NAME NOT IN (SELECT b.COLUMN_NAME FROM “,
case when P_BACKUP_DATABSAE is not null then CONCAT(P_BACKUP_DATABSAE) else “” END,
” b WHERE “,
case when P_TABLE_NAME is not null then CONCAT(“b.TABLE_NAME = “””,P_TABLE_NAME,””””) else “” end,
case when P_TABLE_SCHEMA is not null then CONCAT(” AND b.TABLE_SCHEMA = “””,P_TABLE_SCHEMA,”””)”) ELSE “” end);
PREPARE tsql FROM @tsql;
EXECUTE tsql;
deallocate prepare tsql;
IF @cnt >0 THEN
DROP TEMPORARY TABLE IF EXISTS tmp_table;
create temporary table tmp_table(p_int int AUTO_INCREMENT primary key,p_COLUMN_NAME varchar(100) ,p_DATA_TYPE VARCHAR(100) );
set @tsql2 = CONCAT(“INSERT INTO tmp_table (p_COLUMN_NAME,p_DATA_TYPE) SELECT a.COLUMN_NAME,a.COLUMN_TYPE FROM information_schema.COLUMNS a WHERE “,
case when P_TABLE_NAME is not null then CONCAT(“a.TABLE_NAME = “””,P_TABLE_NAME,””””) else “” end,
case when P_TABLE_SCHEMA is not null then CONCAT(” AND a.TABLE_SCHEMA = “””,P_TABLE_SCHEMA,””””) ELSE “” end,
” AND a.COLUMN_NAME NOT IN (SELECT b.COLUMN_NAME FROM “,
case when P_BACKUP_DATABSAE is not null then CONCAT(P_BACKUP_DATABSAE) else “” END,
” b WHERE “,
case when P_TABLE_NAME is not null then CONCAT(“b.TABLE_NAME = “””,P_TABLE_NAME,””””) else “” end,
case when P_TABLE_SCHEMA is not null then CONCAT(” AND b.TABLE_SCHEMA = “””,P_TABLE_SCHEMA,”””)”) ELSE “” end);
PREPARE tsql2 FROM @tsql2;
EXECUTE tsql2;
deallocate prepare tsql2;
select max(p_int) into cnt from tmp_table;
WHILE i < cnt DO
select p_COLUMN_NAME,p_COLUMN_NAME into TABLE_i,COLUMN_i from tmp_table where p_int=i;
set @tsql3=CONCAT(“alter table “,P_TABLE_NAME,” add “,TABLE_i,char(10),COLUMN_i);
PREPARE tsql3 FROM @tsql3;
EXECUTE tsql3;
DEALLOCATE PREPARE tsql3;
set i=i+1;
end WHILE;
select @tsql3;
end if;
END$$
100
错误显示是下面这个语句的语法有问题
case when P_TABLE_NAME is not null then CONCAT(“a.TABLE_NAME = “””,P_TABLE_NAME,””””) else “” end,
concat函数的格式如下:
CONCAT(str1,str2,…)
CONCAT(“1″,”2″,”3”,””…)
LZ改成之后试一下