mysql存储过程

1、sql

DROP TABLE IF EXISTS `demo`;
CREATE TABLE `demo`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 39 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of demo
-- ----------------------------
INSERT INTO `demo` VALUES (1, 测试);
INSERT INTO `demo` VALUES (2, 测试2);
INSERT INTO `demo` VALUES (3, 测试3);
INSERT INTO `demo` VALUES (32, 测试2);
INSERT INTO `demo` VALUES (33, 测试2);
INSERT INTO `demo` VALUES (34, NULL);
INSERT INTO `demo` VALUES (35, 测试3);
INSERT INTO `demo` VALUES (36, 测试3);
INSERT INTO `demo` VALUES (37, 测试3);
INSERT INTO `demo` VALUES (38, xxx);

SET FOREIGN_KEY_CHECKS = 1;

2、无参数存储过程

DROP PROCEDURE IF EXISTS PRO;
DELIMITER $$
CREATE PROCEDURE PRO()
BEGIN
    SELECT * FROM DEMO;
END$$
DELIMITER;

调用存储过程
CALL PRO()

3、输入参数的存储过程

DROP PROCEDURE IF EXISTS PRO;
DELIMITER $$
CREATE PROCEDURE PRO(IN x int)
BEGIN
    SELECT * FROM DEMO WHERE ID = x;
END$$
DELIMITER;

调用存储过程
CALL PRO(2)

4、输入输出参数的存储过程

DROP PROCEDURE IF EXISTS PRO;
DELIMITER $$
CREATE PROCEDURE PRO(IN x int, OUT res VARCHAR(64))
BEGIN
    SELECT NAME INTO res FROM DEMO WHERE ID = x;
END$$
DELIMITER;

调用存储过程
CALL PRO(2, @res);
SELECT @res;

 

mysql存储过程

上一篇:Windows Azure 配置多个站点的虚拟网络连接


下一篇:mysql 修改大表字段,报错ERROR 1878 (HY000): Temporary file write failure. 用pt-online-schema-change