标签:utf8 proc ext sql ali for ble mil roc
mysql 的存储过程
一、准备工作
新建一个表
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Version : 50505
Source Host : localhost:3306
Source Database : kamanage
Target Server Type : MYSQL
Target Server Version : 50505
File Encoding : 65001
Date: 2018-08-31 17:43:23
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `temp_user`
-- ----------------------------
DROP TABLE IF EXISTS `temp_user`;
CREATE TABLE `temp_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of temp_user
-- ----------------------------
INSERT INTO `temp_user` VALUES (‘4‘, ‘zhanghao‘, ‘2‘);
INSERT INTO `temp_user` VALUES (‘5‘, ‘sql‘, ‘30‘);
INSERT INTO `temp_user` VALUES (‘6‘, ‘pengpeng‘, ‘30‘);
二、存储过程使用的范例:查找到该表里的所有数据,并删除
################################适用于多字段###############################
create PROCEDURE delete_data()
begin
declare name1 char(30);
declare age1 int (3);
declare end_tag int default 0;#循环结束标志 默认是0,代表0的时候都可以循环
declare list CURSOR for select name,age from temp_user;
declare CONTINUE HANDLER for not FOUND set end_tag=1 ;#为 未找到集合 生命处理程序 ,把循环结束标志设置为1
open list; ###打开游标
repeat ###循环开始
FETCH list into name1,age1;#游标开始一个一个滑动
IF not end_tag then ### if not !=1 结束符!=1 就删除
delete from temp_user where name=name1 and age=age1;
end if;
UNTIL end_tag end repeat;#直到 end_tag=1(循环结束符值为1的时候)结束循环
end ;
call delete_data();####调用存储过程
drop procedure delete_data;####删除存储过程
标签:utf8 proc ext sql ali for ble mil roc
原文地址:https://www.cnblogs.com/songqiaoli/p/9567172.html