存储过程使用字符串拼接SQL并执行

CREATE DEFINER=`liulibo`@`%` PROCEDURE `test`()
BEGIN
DECLARE USER_ID_STRING VARCHAR(64) DEFAULT 'WHERE user_id=1';
DECLARE LIMIT_STRING INT DEFAULT 5;
DECLARE SQL_STRING_RUN VARCHAR(200);
SET @SQL_STRING_RUN =  CONCAT("SELECT * FROM user ",USER_ID_STRING," LIMIT ",LIMIT_STRING,";");
PREPARE SQL_STRING_RUN FROM @SQL_STRING_RUN;
EXECUTE SQL_STRING_RUN;
END