MySQL variable declaration and stored procedure analysis

MySQL variable declaration and stored procedure analysis

Declaring variables

Setting Global Variables

set @a='a new variable';

Declear variables used in functions and stored procedures

declear a int unsigned default 1;

This type of variable needs to be set and only exists within the begin..end section.

select .. into.. directly assigns the contents of the table to the specified variable

select name,bid into @a,@b from bank limit 1;

One thing to note is that the variable name cannot be the same as the field name

Stored Procedures

The stored procedure encapsulates a common operation so that it can be used on different platforms.

The stored procedure has no return value and cannot be called by SQL statements. It can only be called by call and does not return a result set. It is executed when executed.

It should be noted that the system default terminator must be reset to something else when executing SQL statements in a stored procedure. Otherwise, the system will mistakenly recognize the program as terminated and report an error halfway through the writing process.

Change the end command character to $

delimiter$+Enter or abbreviated as \d $+Enter

Show all stored procedures

show procedure status;

Delete the specified stored procedure

drop procedure procedure name;

Stored procedure demo'

\d $ 1 create procedure yanshi(in arg tinyint)
begin
declare age tinyint default 0;
set age=arg;
if age<20 then
select 'number less than 20';
elseif age>20 then
select 'number greater than 20';
end if;
end
$
//Calling procedure set @num=12$
call yanshi(@num)$
call yanshi(21)$

Determine which stage the number entered into the stored procedure belongs to

There are three types of parameter passing in stored procedures : in, out, and inout

in can output variables passed in from the outside without changing the original value of the variable passed in

create procedure a(in id int)
begin
  select id;
  set id = 100;
end
$
set @id=1$
call a(@id)$ //output 1, which is the value of @id passed in from outside select $id$ //output 1, indicating that the value passed in has not been changed during the storage process

out cannot output the value passed in from the outside and will change the original value of the variable passed in

create procedure b(out id int)
begin
  select id;
  set id = 100;
end
$
set @id=1$
call b(@id)$ // input null
select @id$ //output 100

inout can both output the passed variable and change the passed variable

Now it's time to check your computer's hardware performance

Remember the bank table from that year? He saved it and executed the following command:

create procedure addbank()
begin
  declare i int default 0;
  set i = 5000000;
  while i > 0 do
  insert into bank (name) values ​​(i);
  set i = i - 1;
  end while;
end
$
call addbank()$

Good luck

Summarize

The above is all the content of this article about MySQL variable declaration and stored procedure analysis. I hope it will be helpful to everyone. Interested friends can refer to: Several important MySQL variables, detailed explanation of MySQL prepare principles, analysis of key points of ORACLE SQL statement optimization techniques, etc. If you have any questions, you can leave a message at any time and the editor will reply to you in time. Thank you friends for your support of 123WORDPRESS.COM!

You may also be interested in:
  • MySQL 8.0.12 installation and environment variable configuration tutorial under win10
  • Detailed explanation of two methods for setting global variables and session variables in MySQL
  • MySQL 5.6.23 Installation and Configuration Environment Variables Tutorial
  • MySQL 8 new features: how to modify persistent global variables
  • Code analysis of user variables in mysql query statements
  • Several important MySQL variables
  • MySQL uses variables to implement various sorting
  • A brief discussion on the difference between declare and set variables in MySQL stored procedures
  • MySQL variable principles and application examples

<<:  Detailed explanation of viewing and setting file permissions on Mac

>>:  Detailed explanation of upgrading Python and installing pip under Linux

Recommend

Troubleshooting the cause of 502 bad gateway error on nginx server

The server reports an error 502 when synchronizin...

Introduction and use of Javascript generator

What is a generator? A generator is some code tha...

Nodejs uses readline to prompt for content input example code

Table of contents Preface 1. bat executes js 2. T...

CentOS7.5 installation of MySQL8.0.19 tutorial detailed instructions

1. Introduction This article does not have screen...

Complete steps for vue dynamic binding icons

0 Differences between icons and images Icons are ...

Solution to EF (Entity Framework) inserting or updating data errors

Error message: Store update, insert, or delete st...

Mysql join table and id auto-increment example analysis

How to write join If you use left join, is the ta...

Vue realizes screen adaptation of large screen pages

This article shares the specific code of Vue to a...

SQL Get stored procedure return data process analysis

This article mainly introduces the analysis of th...

The implementation process of long pressing to identify QR code in WeChat applet

Preface We all know that the QR codes in official...

Detailed explanation of the new CSS display:box property

1. display:box; Setting this property on an eleme...

Linux kernel device driver kernel time management notes

/****************** * Linux kernel time managemen...

7 Best VSCode Extensions for Vue Developers

Adding the right VS Code extension to Visual Stud...