当前位置:首页 > 开发 > 数据库 > 正文

mysql 写过的存储过程(PROCEDURE)

发表于: 2012-09-04   作者:ling凌yue月   来源:转载   浏览:
摘要: 因为在自定义函数中limit后面不支持写变量,也不支持动态sql,所以只能写一个存储过程。 getID: delimiter $$ DROP PROCEDURE IF EXISTS `getID`$$ CREATE PROCEDURE getID(IN appID INT, IN tID INT, IN floor INT) BEGIN DECLARE s VARCH
因为在自定义函数中limit后面不支持写变量,也不支持动态sql,所以只能写一个存储过程。

getID:
delimiter $$
DROP PROCEDURE IF EXISTS `getID`$$
CREATE PROCEDURE getID(IN appID INT, IN tID INT, IN floor INT)
BEGIN
	DECLARE s VARCHAR(255);
	DECLARE f INT;
	SET f = floor - 1;
	SET @mID = 0;
	SET @ms=concat('SELECT `id` INTO @mID FROM `comment`  WHERE `appid`= ',appID,'
 AND `tid`= ',tID,' ORDER BY `id` LIMIT ',f,',1');
	PREPARE STMT FROM @ms;
	EXECUTE STMT;
END
$$
delimiter ;


第二种写法:
delimiter $$
DROP PROCEDURE IF EXISTS `getID`$$
CREATE PROCEDURE getID(IN appID INT, IN tID INT, IN floor INT)
BEGIN
	DECLARE f INT;
	SET f = floor - 1;
	SET @appID = appID;
	SET @tID = tID;
	SET @f = f;
	SET @mID = 0;
	SET @ms = 'SELECT `id` INTO @mID FROM `wy_comment`  WHERE `appid`=?
 AND `tid`=? ORDER BY `id` LIMIT ?,1';
	PREPARE STMT FROM @ms;
	EXECUTE STMT USING @appID,@tID,@f;
END
$$
delimiter ;


测试sql:
CALL getID(25,3399,2);

SELECT @mID;


mysql 写过的存储过程(PROCEDURE)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号