Home > Database > Mysql Tutorial > Quick Start with MySQL Stored Procedures

Quick Start with MySQL Stored Procedures

巴扎黑
Release: 2017-04-15 09:08:41
Original
1390 people have browsed it

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
Copy after login
Copy after login

Give me an example

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

                                      ​ ​ ​ ​


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
Copy after login
Copy after login

Give me an example

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


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template