這是我項目當中用到的,用作參考使用:
DELIMITER $$ DROP PROCEDURE IF EXISTS `gpsdata`.`sp_test`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_test`( gpstime_ varchar(128), gpsname_ varchar(128), gpsinfo_ varchar(256) ) BEGIN DECLARE tbname varchar(50) DEFAULT '0'; DECLARE v_sql varchar(1024) DEFAULT '0'; SET v_sql=CONCAT('select * from ', tbname ,' where gpsname = ',gpsname_,' order by gpstime desc limit 1'); SET @lastdata = v_sql; PREPARE lastdata FROM @lastdata; EXECUTE lastdata; DEALLOCATE PREPARE lastdata; select v_sql; END$$ DELIMITER ;