MySQL存储过程带in和out参数

系统 1679 0

  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)</p><p>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 | www.sd-ju.com

  +----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

  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; www.js-yg.com

  -> 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)

 

MySQL存储过程带in和out参数


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论