Home  >  Article  >  Database  >  Quick Start with MySQL Stored Procedures

Quick Start with MySQL Stored Procedures

巴扎黑
巴扎黑Original
2017-04-15 09:08:411363browse

To use stored procedures in a database, you must first understand how to use variables and custom functions in the database. The brute force introduction begins.

Variables

The difference between system variables and user-defined variables is that user-defined variables use a
@ when viewed , while system variables use Two@

  • system variables

  1. Introduction: Users cannot define the system Variables, but can be modified and used

  2. Usage: Use select to get the value of the variable, but because select will query all strings as fields of a table by default, if it is a variable You need to use the @@ symbol to access

View all system built-in variables command: show variables;
View a certain System variable command: select @@variable name;, such as select @@version
Modify variable (local modification) command: set variable name = value , such as set autocommit = 3;

  • Custom variable

Custom variable syntax :set @name = value;, such as `set @name = 'saboran';
View the custom variable value: select @name;

  • Variable scope

Externally defined variables are called global variables. Global variables are the same as global variables in js and can be used in Used inside the function;

Local variables: declare variable data type
Local variables cannot be accessed outside the function.

Custom function

Syntax

create function 函数名(参数列表) returns 数据类型
    begin
        // 函数体 
        // 返回值
    end

Give me an example

delimiter $$ 
create function avg(first int) returns int
    begin 
        declare value ;
        set value = first;
        return value;
    end
    $$

                                      ​ ​ ​ ​


MySQL stored procedure brute force introduction

To use stored procedures in the database, you must first understand how to use variables and custom functions in the database. The brute force introduction begins.

Variables

The difference between system variables and user-defined variables is that user-defined variables use a
@ when viewed , while system variables use Two@

  • system variables

  1. Introduction: Users cannot define the system Variables, but can be modified and used

  2. Usage: Use select to get the value of the variable, but because select will query all strings as fields of a table by default, if it is a variable You need to use the @@ symbol to access

View all system built-in variables command: show variables;
View a certain System variable command: select @@variable name;, such as select @@version
Modify variable (local modification) command: set variable name = value , such as set autocommit = 3;

  • Custom variable

Custom variable syntax :set @name = value;, such as `set @name = 'saboran';
View the custom variable value: select @name;

  • Variable scope

Externally defined variables are called global variables. Global variables are the same as global variables in js and can be used in Used inside the function;

Local variables: declare variable data type
Local variables cannot be accessed outside the function.

Custom function

Syntax

create function 函数名(参数列表) returns 数据类型
    begin
        // 函数体 
        // 返回值
    end

Give me an example

delimiter $$ 
create function avg(first int) returns int
    begin 
        declare value ;
        set value = first;
        return value;
    end
    $$


The above is the detailed content of Quick Start with MySQL Stored Procedures. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn