MYSQL存储过程学习

1、游标的使用

BEGIN
    DECLARE done INT DEFAULT 0;
    DECLARE userid INT;
    DECLARE rs CURSOR FOR SELECT `uid` FROM sinbegin_user WHERE service = 0;/*定义游标*/
    DECLARE CONTINUE HANDLER FOR SQLSTATE 02000 SET done = 1;/*异常处理*/
    OPEN rs;/*打开游标*/
        FETCH NEXT FROM rs INTO userid;
        REPEAT/*遍历数据表*/
            IF NOT done THEN
                UPDATE sinbegin_user SET service = 1 WHERE uid = userid;
            END IF;
        FETCH NEXT FROM rs INTO userid;
        UNTIL done END REPEAT;
    CLOSE rs;/*关闭游标*/
END

 2、IF条件判断的使用

语法

IF expression THEN commands
   [ELSEIF expression THEN commands]
   [ELSE commands]
   END IF;

示例代码

IF CurrentLingQi >= 1800 THEN
    UPDATE socialrelation SET LingQi = 1800 WHERE NodeName = ShizuName;
ELSE
     UPDATE socialrelation SET LingQi = LingQi + 60 WHERE NodeName = ShizuName;
END IF;

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。