如何将我的表名作为 MySQL 存储过程的参数进行传递以获取全部记录?
假设我们需要查看某个表的全部记录,但将表名作为存储过程的参数进行传递,那么以下示例将会创建一个名为“详情”的过程,该过程会将表名作为其参数来接受 -
mysql> DELIMITER // mysql> Create procedure details(tab_name Varchar(40)) -> BEGIN -> SET @t:= CONCAT('Select * from',' ',tab_name); -> Prepare stmt FROM @t; -> EXECUTE stmt; -> END // Query OK, 0 rows affected (0.00 sec)
现在,将表名作为其参数来调用此过程,而它将会显示该表的全部记录。
mysql> DELIMITER; mysql> CALL details('student_detail'); +-----------+-------------+------------+ | Studentid | StudentName | address | +-----------+-------------+------------+ | 100 | Gaurav | Delhi | | 101 | Raman | Shimla | | 103 | Rahul | Jaipur | | 104 | Ram | Chandigarh | | 105 | Mohan | Chandigarh | +-----------+-------------+------------+ 5 rows in set (0.02 sec) Query OK, 0 rows affected (0.03 sec)
广告