In MySQL: How to pass a table name as stored procedure and/or function argument?
You can do it like this:
DROP PROCEDURE IF EXISTS `getDataUsingSiteCode`;
DELIMITER $$
CREATE PROCEDURE `getDataUsingSiteCode`(
IN tab_name VARCHAR(40),
IN site_ VARCHAR(255)
)
BEGIN
SET @site_code = site_;
SET @sql_ =CONCAT('SELECT * FROM ',tab_name,' WHERE site=?');
PREPARE statement_ FROM @sql_;
EXECUTE statement_ using @site_code;
END$$
DELIMITER ;
Prepared statements are what you need.
CREATE PROCEDURE `test1`(IN tab_name VARCHAR(40) )
BEGIN
SET @t1 =CONCAT('SELECT * FROM ',tab_name );
PREPARE stmt3 FROM @t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
END $$