mysql存储过程不支持动态sql 怎么办
时间: 2024-01-08 12:17:42
浏览: 48
可以尝试使用PREPARE和EXECUTE语句来实现[动态](https://geek.csdn.net/educolumn/08aef4ab97997f5b9ed71860c44e8258?spm=1055.2569.3001.10083)SQL。先用PREPARE将SQL语句准备好,然后再用EXECUTE执行。
DELIMITER $$
CREATE [proc](https://geek.csdn.net/educolumn/535f08ce32f749725902114bc56ad7b8?spm=1055.2569.3001.10083)EDURE dynamic_query(IN tab[len](https://geek.csdn.net/educolumn/57d1c7022a5d69dfb6174ca472b0ff65?spm=1055.2569.3001.10083)ame VARCHAR(64))
BE[gin](https://geek.csdn.net/educolumn/1aef582107c22aa4d9f243890549bf39?spm=1055.2569.3001.10083)
SET @sql = CONCAT('SELECT * FROM ', tab[len](https://geek.csdn.net/educolumn/57d1c7022a5d69dfb6174ca472b0ff65?spm=1055.2569.3001.10083)ame);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;