版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a19990412/article/details/84996303
简述
看到下文受到启发,解决了这个问题
https://blog.csdn.net/hengbao4/article/details/78084008
代码
mysql> delimiter //
mysql> create procedure print(in t varchar(50))
-> begin
-> SET @tbl_name=CONCAT("",t);
-> SET ^C
mysql> create procedure print(in t varchar(50))
-> begin
-> SET @tname=CONCAT("",t);
-> SET @state=CONCAT("select * from ", @tname);
-> prepare stmt from @state;
-> execute stmt;
-> end//
Query OK, 0 rows affected (0.06 sec)
- 这样就创建了一个print存储过程
- 调用方式如下:
mysql> call print('student')//
+---------+--------+------+------+-------+
| sno | sname | ssex | sage | sdept |
+---------+--------+------+------+-------+
| 2005001 | 钱横 | 男 | 18 | Cs |
| 2005002 | 王林 | 女 | 19 | Cs |
| 2005003 | 李民 | 男 | 20 | Is |
| 2005004 | 赵欣然 | 女 | 16 | Ma |
| 2005007 | 李涛 | 男 | 19 | Is |
| 2005008 | 陈高 | 女 | 21 | At |
| 2005009 | 张杰 | 男 | 17 | At |
| 2005010 | 赵青江 | 男 | 18 | Cs |
| 2005011 | 张丽萍 | 女 | 19 | Ch |
| 2005012 | 陈景欢 | 男 | 20 | Is |
| 2005013 | 陈婷婷 | 女 | 16 | Ph |
| 2005014 | 李军 | 女 | 16 | Ph |
+---------+--------+------+------+-------+
12 rows in set (0.02 sec)