mysql function动态执行不同sql语句


create
procedure cps() begin declare table_user varchar(200) default 'user'; set strSql = concat('select * from ',table_user); prepare a from strSql; execute a; end

这个过程中会出现错误。

 

如何使用sql执行字符串?

需要使用prepare  预编译sql

prepare a from 'select * from food';
 execute a; 

这样是可以的

 

如果那条语句是可变的呢?那么需要变量保存部分信息,变量有什么要求吗?

 

所以,如果要使用SESSION级别变量的话,变量前一定要加@符号

 

当然,使用prepare也可以这样使用参数:

 

 

 

 我这个是找问题的时候找的资料的一个集合版,参考链接:

http://bbs.csdn.net/topics/350158591

posted @ 2017-03-31 15:19  guodaxia  阅读(1665)  评论(0编辑  收藏  举报