----------------------------------Example CREATE TABLE t1 (f1 varchar(1), f2 varchar(1), f3 varchar(1)); INSERT INTO t1 (f1, f2, f3) VALUES ('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'), ('j', 'k', 'l'); DROP PROCEDURE IF EXISTS p_tst; DELIMITER // CREATE PROCEDURE p_tst(in_f1 varchar(1), in_f2 varchar(1), in_f3 varchar(1)) BEGIN DECLARE done INT DEFAULT 0; DECLARE v_ustat_criteria_lu_id MEDIUMINT UNSIGNED; DECLARE v_criteria_short_desc VARCHAR(30); DECLARE cur_ustat CURSOR FOR SELECT * FROM t1; DECLARE continue handler for sqlstate '02000' set done = 1; DROP TABLE IF EXISTS t2; SET @s = 'CREATE TABLE t2 AS SELECT '; IF in_f1 = 'Y' THEN SET @s = CONCAT(@s, 'f1, '); END IF; IF in_f2 = 'Y' THEN SET @s = CONCAT(@s, 'f2, '); END IF; IF in_f3 = 'Y' THEN SET @s = CONCAT(@s, 'f3, '); END IF; SET @s = SUBSTRING(@s, 1, length(@s) -2); SET @s = CONCAT(@s, ' FROM t1'); SELECT @s; PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt; SELECT @s; SELECT * FROM t2; END // DELIMITER ; -- After creating procedure, the first call to it always work correctly. -- 1a) This call specifies that the table will be created with just the f1 column. mysql> CALL p_tst('Y', 'N', 'N'); +-----------------------------------------------+ | @s | +-----------------------------------------------+ | CREATE TABLE t2 AS SELECT f1 FROM t1 | +-----------------------------------------------+ 1 row in set (0.00 sec) +-----------------------------------------------+ | @s | +-----------------------------------------------+ | CREATE TABLE t2 AS SELECT f1 FROM t1 | +-----------------------------------------------+ 1 row in set (0.05 sec) +------+ | f1 | +------+ | a | | d | | g | | j | +------+ 4 rows in set (0.05 sec) Query OK, 0 rows affected (0.05 sec) -- 1b) This call specifies that table t2 will contain same f1 column as above -- plus additional columns f2 and f3. This will return incorrect results. mysql> CALL p_tst('Y', 'Y', 'Y'); +-------------------------------------------------------+ | @s | +-------------------------------------------------------+ | CREATE TABLE t2 AS SELECT f1, f2, f3 FROM t1 | +-------------------------------------------------------+ 1 row in set (0.00 sec) +-------------------------------------------------------+ | @s | +-------------------------------------------------------+ | CREATE TABLE t2 AS SELECT f1, f2, f3 FROM t1 | +-------------------------------------------------------+ 1 row in set (0.02 sec) +------+ | f1 | +------+ | a | | d | | g | | j | +------+ 4 rows in set (0.02 sec) Query OK, 0 rows affected (0.02 sec) -- Although the CREATE statement for t2 showed 3 columns, only 1 column -- was returned from SELECT * FROM t2, which is incorrect. -- If I run the same SELECT statement at the command line, it works correctly. mysql> SELECT * FROM t2; +------+------+------+ | f1 | f2 | f3 | +------+------+------+ | a | b | c | | d | e | f | | g | h | i | | j | k | l | +------+------+------+ 4 rows in set (0.00 sec) -- DROP: Now I drop and recreate the procedure and start again. This is the only -- way I've found to get the procedure to "reset" and return the correct results. -- 2a) This time I start with a call that specifies that the new table will have -- all 3 columns. This works correctly. Note that this didn't work above. mysql> CALL p_tst('Y', 'Y', 'Y'); +-------------------------------------------------------+ | @s | +-------------------------------------------------------+ | CREATE TABLE t2 AS SELECT f1, f2, f3 FROM t1 | +-------------------------------------------------------+ 1 row in set (0.00 sec) +-------------------------------------------------------+ | @s | +-------------------------------------------------------+ | CREATE TABLE t2 AS SELECT f1, f2, f3 FROM t1 | +-------------------------------------------------------+ 1 row in set (0.03 sec) +------+------+------+ | f1 | f2 | f3 | +------+------+------+ | a | b | c | | d | e | f | | g | h | i | | j | k | l | +------+------+------+ 4 rows in set (0.03 sec) Query OK, 0 rows affected (0.03 sec) -- 2b) But if I now specify that table t2 should have just one of these -- columns, I get an error. mysql> CALL p_tst('Y', 'N', 'N'); +-----------------------------------------------+ | @s | +-----------------------------------------------+ | CREATE TABLE t2 AS SELECT f1 FROM t1 | +-----------------------------------------------+ 1 row in set (0.00 sec) +-----------------------------------------------+ | @s | +-----------------------------------------------+ | CREATE TABLE t2 AS SELECT f1 FROM t1 | +-----------------------------------------------+ 1 row in set (0.03 sec) ERROR 1054 (42S22): Unknown column '[db name].t2.f2' in 'field list' -- But again, if I run the same query at the command line, I get -- the correct results and no error. mysql> SELECT * FROM t2; +------+ | f1 | +------+ | a | | d | | g | | j | +------+ 4 rows in set (0.01 sec) mysql> desc t2; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | f1 | varchar(1) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 1 row in set (0.01 sec)