第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

MySQL存儲(chǔ)過(guò)程變量

標(biāo)簽:
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  DECLARE keyword. 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 INTVARCHARDATETIME, 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_sale with the data type INT and 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 DECLARE statement as following:

DECLARE x, y INT DEFAULT 0

We declared two INT variables  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_count variable is 10 after the assignment.

Besides the SET statement, 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_products and initialize its value to 0.

  • Then, we used the SELECT INTO statement to assign the total_products variable 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 END statement of stored procedure reached.

If you declare a variable inside BEGIN END block, it will be out of scope if the END is 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

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

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消