MySQL存储过程带in和out参数_MySQL
程序员文章站
2022-03-08 10:12:44
...
bitsCN.com
bitsCN.com
MySQL存储过程带in和out参数
最简单的例子:
[html] mysql> DELIMITER $$ mysql> USE test $$ Database changed mysql> DROP PROCEDURE IF EXISTS `sp_add`$$ Query OK, 0 rows affected (0.00 sec) mysql> CREATE PROCEDURE sp_add(a INT, b INT,OUT c INT) -> BEGIN -> SET c=a+ b; -> END$$ Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER ;
[html] mysql> CALL sp_add (1,2,@c); Query OK, 0 rows affected (0.00 sec)mysql> SELECT @c; +------+ | @c | +------+ | 3 | +------+ 1 row in set (0.00 sec)
一个稍微复杂的例子:
[html] mysql> show create table t_BillNo; +----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | t_BillNo | CREATE TABLE `t_billno` ( `SaleNo` bigint(20) DEFAULT NULL, `bmh` varchar(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC | +----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> select * from t_BillNo; +--------+------+ | SaleNo | bmh | +--------+------+ | 1 | 2 | | 4 | 3 | | 4 | 5 | | 7 | 7 | | 12 | 8 | +--------+------+ 5 rows in set (0.00 sec) mysql> mysql> DELIMITER $$ mysql> USE test $$ Database changed mysql> DROP PROCEDURE IF EXISTS `sp_GetMaxNumber`$$ Query OK, 0 rows affected (0.01 sec) DELIMITER $$ USE test $$ DROP PROCEDURE IF EXISTS `sp_GetMaxNumber`$$ CREATE PROCEDURE sp_GetMaxNumber (IN v_bmh VARCHAR(6), OUT v_MaxNo INT) BEGIN START TRANSACTION; UPDATE t_BillNo SET SaleNo = IFNULL(SaleNo,0)+1 WHERE bmh = v_bmh; IF @@error_count = 0 THEN BEGIN SELECT Saleno INTO v_MaxNo FROM t_BillNo WHERE bmh = v_bmh; COMMIT; END; ELSE BEGIN ROLLBACK; SET v_MaxNo = 0; END; END IF; END$$ DELIMITER ; mysql> CREATE PROCEDURE sp_GetMaxNumber (IN v_bmh VARCHAR(6), OUT v_MaxNo INT) -> BEGIN -> START TRANSACTION; -> UPDATE t_BillNo -> SET SaleNo = IFNULL(SaleNo,0)+1 -> WHERE bmh = v_bmh; -> IF @@error_count = 0 THEN -> BEGIN -> SELECT Saleno INTO v_MaxNo FROM t_BillNo WHERE bmh = v_bmh; -> COMMIT; -> END; -> ELSE -> BEGIN -> ROLLBACK; -> SET v_MaxNo = 0; -> END; -> END IF; -> END$$ Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER ; mysql> mysql> call sp_GetMaxNumber(8,@v_MaxNo); Query OK, 0 rows affected (0.00 sec) mysql> select @v_MaxNo; +----------+ | @v_MaxNo | +----------+ | 12 | +----------+ 1 row in set (0.00 sec)
bitsCN.com
上一篇: div滚动条的颜色设置以及一些问题的总结
下一篇: div jquyer 上下切换位置
推荐阅读
-
mysql 开发基础系列17 存储过程和函数(上)
-
MySQL数据库存储过程和事务的区别讲解
-
MySQL 存储过程和"Cursor"的使用方法
-
mysql存储过程之循环语句(WHILE,REPEAT和LOOP)用法分析
-
mysql存储过程之创建(CREATE PROCEDURE)和调用(CALL)及变量创建(DECLARE)和赋值(SET)操作方法
-
mysql存储过程之引发存储过程中的错误条件(SIGNAL和RESIGNAL语句)实例分析
-
mysql表名作为参数传入存储过程
-
MYSQL存储过程中事务和DECLARE EXIT/CONTINUE HANDLER的使用
-
细谈Mysql的存储过程和存储函数
-
MySql存储过程中的基本函数和需要用到的运算符