为了账号安全,请及时绑定邮箱和手机立即绑定

MySQL存储过程参数

标签:
MySQL

Summary: in this tutorial, we will show you how to write MySQL stored procedures with parameters. We will also give you a couple of stored procedure examples to help you understand how to use different kinds of stored procedure parameters.

Introduction to MySQL stored procedure parameters

Almost stored procedures that you develop require parameters. The parameters make the stored procedure more flexible and useful. In MySQL, a parameter has one of three modes INOUT or INOUT.

  • IN – is the default mode. When you define an IN parameter in a stored procedure, the calling program has to pass an argument to the stored procedure. In addition, the value of an IN parameter is protected. It means that even the value of the IN parameter is changed inside the stored procedure, its original value is retained after the stored procedure ends. In other words, the stored procedure only works on the copy of the IN parameter.

  • OUT – the value of an OUT parameter can be changed inside the stored procedure and its new value is passed back to the calling program. Notice that the stored procedure cannot access the initial value of the OUT parameter when it starts.

  • INOUT – an INOUT parameter is the combination of IN parameter and OUT parameter. It means that the calling program may pass the argument, and the stored procedure can modify the INOUT parameter and pass the new value back to the calling program.

The syntax of defining a parameter in the stored procedures is as follows:

MODE param_name param_type(param_size)

 

  • The MODE could be INOUT or INOUT , depending on the purpose of parameter in the stored procedure.

  • The param_name is the name of the parameter. The name of parameter must follow the naming rules of the column name in MySQL.

  • Followed the parameter name is its data type and size. Like a variable, the data type of the parameter can by any MySQL data type.

Each parameter is separated by a comma ( ,) if the stored procedure has more than one parameter.

Let’s practice with some examples to get a better understanding.

MySQL stored procedure parameter examples

IN parameter example

The following example illustrates how to use the IN parameter in the GetOfficeByCountry stored procedure that selects offices located in a specified country.

DELIMITER // CREATE PROCEDURE GetOfficeByCountry(IN countryName VARCHAR(255)) BEGIN SELECT *    FROM offices   WHERE country = countryName; END // DELIMITER ;

The countryName is the IN parameter of the stored procedure. Inside the stored procedure, we select all offices that locate in the country specified by the countryName parameter.

Suppose, you want to get all offices in the USA, you just need to pass a value (USA) to the stored procedure as follows:

CALL GetOfficeByCountry('USA')

MySQL Stored Procedure Parameters - IN parameter example

To get all offices in France, you pass the France literal string to the GetOfficeByCountry stored procedure as follows:

CALL GetOfficeByCountry('France')

IN parameter offices in France

OUT parameter example

The following stored procedure returns the number of orders by order status. It has two parameters:

  • orderStatusIN parameter that is the order status which you want to count the orders.

  • totalOUT parameter that stores the number of orders for a specific order status.

The following is the source code of the CountOrderByStatus stored procedure.

DELIMITER $$ CREATE PROCEDURE CountOrderByStatus(   IN orderStatus VARCHAR(25),   OUT total INT) BEGIN SELECT count(orderNumber) INTO total FROM orders WHERE status = orderStatus; END$$ DELIMITER ;

To get the number of shipped orders, we call the CountOrderByStatus stored procedure and pass the order status as Shipped, and also pass an argument ( @total) to get the return value.

CALL CountOrderByStatus('Shipped',@total); SELECT @total;

MySQL Stored Procedure Parameters - OUT parameter order shipped

To get the number of orders that are in process, we call the CountOrderByStatus stored procedure as follows:

CALL CountOrderByStatus('in process',@total); SELECT @total AS  total_in_process;

MySQL Stored Procedure Parameters - OUT parameter orders in process

INOUT parameter example

The following example demonstrates how to use INOUT parameter in the stored procedure.

DELIMITER $$ CREATE PROCEDURE set_counter(INOUT count INT(4),IN inc INT(4)) BEGIN SET count = count + inc; END$$ DELIMITER ;

How it works.

  • The set_counter stored procedure accepts one INOUT parameter ( count) and one IN parameter ( inc).

  • Inside the stored procedure, we increase the counter ( count) by the value of the inc parameter.

See how we call the set_counter stored procedure:

SET @counter = 1; CALL set_counter(@counter,1); -- 2 CALL set_counter(@counter,1); -- 3 CALL set_counter(@counter,5); -- 8 SELECT @counter; -- 8

In this tutorial, we have shown you how to define parameters in stored procedures, and introduced you to different parameter modes including INOUT and INOUT.

Related Tutorials

原文链接:http://outofmemory.cn/mysql/procedure/stored-procedures-parameters

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消