mysql 存储过程 with_MySQL存储过程变量

Summary: in this tutorial, you will learn about variables in stored procedure, how to declare, and use variables. In addition, you will learn about the scopes of variables.

A variable is a named data object whose value can change during the stored procedure execution. We typically use the variables in stored procedures to hold the immediate results. These variables are local to the stored procedure.

You must declare a variable before you can use it.

Declaring variables

To declare a variable inside a stored procedure, you use the DECLARE statement as follows:DECLARE variable_name datatype(size) DEFAULT default_value;

Let’s examine the statement above in more detail:First, you specify the variable name after the  DECLAREkeyword. The variable name must follow the naming rules of MySQL table column names.

Second, you specify the data type of the variable and its size. A variable can have any MySQL data types such as INT, VARCHAR, DATETIME, etc.

Third, when you declare a variable, its initial value is NULL. You can assign the variable a default value by using DEFAULTkeyword.

For example, we can declare a variable named  total_salewith the data type INTand default value 0 as follows:DECLARE total_sale INT DEFAULT 0

MySQL allows you to declare two or more variables that share the same data type using a single DECLAREstatement as following:DECLARE x, y INT DEFAULT 0

We declared two INTvariables  x and  y , and set their default values to zero.

Assigning variables

Once you declared a variable, you can start using it. To assign a variable another value, you use the SET statement, for example:DECLARE total_count INT DEFAULT 0 SET total_count = 10;

The value of the total_countvariable is 10 after the assignment.

Besides the SETstatement, you can use SELECT INTO statement to assign the result of a query to a variable. Notice that the query must return a scalar value.DECLARE total_products INT DEFAULT 0 SELECT COUNT(*) INTO total_products FROM products

In the example above:First, we declare a variable named total_productsand initialize its value to 0.

Then, we used the SELECT INTO statement to assign the total_productsvariable the number of products that we selected from the products from the products table.

Variables scope

A variable has its own scope, which defines its life time. If you declare a variable inside a stored procedure, it will be out of scope when the ENDstatement of stored procedure reached.

If you declare a variable inside BEGIN END block, it will be out of scope if the ENDis reached. You can declare two or more variables with the same name in different scopes because a variable is only effective in its own scope. However, declaring variables with the same name in different scopes is not good programming practice.

A variable that begins with the @sign at the beginning is session variable. It is available and accessible until the session ends.

In this tutorial, we have shown you how to declare a variable inside stored procedures and discussed about the variable scopes.

Related Tutorials

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值