I have a mySQL database called listDB that contain several tables with column name Product etc. I want to SELECT from all tables where
Improve to @inno answer
delimiter //
DROP PROCEDURE IF EXISTS get_product;
CREATE PROCEDURE get_product()
BEGIN
DECLARE i VARCHAR(100);
DECLARE cur1 CURSOR FOR SELECT DISTINCT table_name FROM information_schema.columns WHERE COLUMN_NAME IN ('Product');
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO i;
SELECT i; -- printing table name
SET @s = CONCAT('select * from ', i, ' where Product like %XYZ%');
PREPARE stmt1 FROM @s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END LOOP read_loop;
CLOSE cur1;
END//
delimiter ;
call get_product();