MySQL declare statement usage

Source: Internet
Author: User

The MySQL declare statement is a commonly used statement. The following describes the usage of the MySQL declare statement. It is helpful for you to learn how to use the MySQL declare statement.

The MySQL declare statement is a command that declares variables in a composite statement.

(1) Example with two DECLARE statements
Instances of two DECLARE statements

 
 
  1. CREATE PROCEDURE p8 ()   
  2. BEGIN   
  3. DECLARE a INT;   
  4. DECLARE b INT;   
  5. SET a = 5;   
  6. SET b = 5;   
  7. INSERT INTO t VALUES (a);   
  8. SELECT s1 * a FROM t WHERE s1 >= b;   
  9. END; // /* I won't CALL this */   

Variables defined in the process are not really defined. You just defined them in the BEGIN/END block ). Note that these variables are different from session variables. You cannot use the modifier @. You must clearly declare the variables and their types in the BEGIN/END block. Once a variable is declared, you can use it wherever you can use session variables, text, and column names.

(2) Example with no DEFAULT clause and SET statement
Example of no default clause or set statement

 
 
  1. CREATE PROCEDURE p9 ()   
  2. BEGIN   
  3. DECLARE a INT /* there is no DEFAULT clause */;   
  4. DECLARE b INT /* there is no DEFAULT clause */;   
  5. SET a = 5; /* there is a SET statement */   
  6. SET b = 5; /* there is a SET statement */   
  7. INSERT INTO t VALUES (a);   
  8. SELECT s1 * a FROM t WHERE s1 >= b;   
  9. END; // /* I won't CALL this */   

There are many methods for initializing variables. If no default clause exists, the initial value of the variable is NULL. You can use the SET statement to assign values to variables at any time.

(3) Example with DEFAULT clause
Example with DEFAULT clause

 
 
  1. CREATE PROCEDURE p10 ()   
  2. BEGIN   
  3. DECLARE a, b INT DEFAULT 5;   
  4. INSERT INTO t VALUES (a);   
  5. SELECT s1 * a FROM t WHERE s1 >= b;   
  6. END; //   

We have made some changes here, but the results are the same. Here, the DEFAULT clause is used to SET the initial value, which does not need to be separated from the implementation of the DECLARE and SET statements.

(4) Example of CALL
Call example

 
 
  1. mysql> CALL p10() //   
  2. +--------+   
  3. | s1 * a |   
  4. +--------+   
  5. | 25 |   
  6. | 25 |   
  7. +--------+   
  8. 2 rows in set (0.00 sec)   
  9. Query OK, 0 rows affected (0.00 sec)   

The result shows that the process works properly.

(5) Scope
Scope

 
 
  1. CREATE PROCEDURE p11 ()   
  2. BEGIN   
  3. DECLARE x1 CHAR(5) DEFAULT 'outer';   
  4. BEGIN   
  5. DECLARE x1 CHAR(5) DEFAULT 'inner';   
  6. SELECT x1;   
  7. END;   
  8. SELECT x1;   
  9. END; //   

Top 10 most noteworthy MySQL Variables

Use of MySQL System Variables

Difference between NULL and MySQL NULL strings

In-depth study of MySQL result strings

MySQL string truncation Function Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.