当前位置:网站首页>MySQL function variable stored procedure
MySQL function variable stored procedure
2022-07-01 04:03:00 【Dull wooden chicken】
mysql Variable
mysql Variable
- System variables
Global variables : For all conversations ( Connect ) It works , But not across reboots
Session variables : For the current session ( Connect ) It works , Scope is the same as session variable - User defined variables
User variables : For the current session ( Connect ) It works , Scope is the same as session variable
local variable : For the current session ( Connect ) It works , Scope is the same as session variable
System variables
explain : Variables are defined by the system , Not user defined , At the server level
Be careful : Global variables need to add global keyword , Session variables need to add session keyword , If you don't write , Default session level
Use steps :
1、 See all system variables
show global|【session】variables;
2、 Look at some of the system variables that meet the criteria
show global|【session】 variables like '%char%';
3、 View the value of the specified system variable
select @@global|【session】 System variable name ;
4、 Assign a value to a system variable
Mode one :
set global|【session】 System variable name = value ;
Mode two :
set @@global|【session】 System variable name = value ;
Global variables
#① See all global variables
SHOW GLOBAL VARIABLES;
#② Look at some of the system variables that meet the criteria
SHOW GLOBAL VARIABLES LIKE '%char%';
#③ View the value of the specified system variable
SELECT @@global.autocommit;
#④ Assign a value to a system variable
SET @@global.autocommit=0;
SET GLOBAL autocommit=0;
Session variables
#2》 Session variables
/*
Scope : For the current session ( Connect ) It works
*/
#① View all session variables
SHOW SESSION VARIABLES;
#② Look at some session variables that meet the criteria
SHOW SESSION VARIABLES LIKE '%char%';
#③ View the value of the specified session variable
SELECT @@autocommit;
SELECT @@session.tx_isolation;
#④ Assign a value to a session variable
SET @@session.tx_isolation='read-uncommitted';
SET SESSION tx_isolation='read-committed';
User variables
explain : Variables are user-defined , Not the system
Use steps :
1、 Statement
2、 assignment
3、 Use ( see 、 Compare 、 Operations, etc )
Scope : For the current session ( Connect ) It works , Scope is the same as session variable
User variables
/*
Scope : For the current session ( Connect ) It works , Scope is the same as session variable
*/
# Assignment operator := or :=
#① Declare and initialize
SET @ Variable name = value ;
SET @ Variable name := value ;
SELECT @ Variable name := value ;
#② assignment ( Update the value of the variable )
# Mode one :
SET @ Variable name = value ;
SET @ Variable name := value ;
SELECT @ Variable name := value ;
# Mode two :
SELECT Field INTO @ Variable name
FROM surface ;
#③ Use ( Look at the value of the variable )
SELECT @ Variable name ;
local variable
#2》 local variable
/*
Scope : Just defining it begin end It works in blocks
Apply to begin end The first sentence in
*/
#① Statement
DECLARE Variable name type ;
DECLARE Variable name type 【DEFAULT value 】;
#② assignment ( Update the value of the variable )
# Mode one :
SET Local variable name = value ;
SET Local variable name := value ;
SELECT Local variable name := value ;
# Mode two :
SELECT Field INTO Have variable names
FROM surface ;
#③ Use ( Look at the value of the variable )
SELECT Local variable name ;
Example declaration print sum of two variables
# User variables
SET @m=1;
SET @n=1;
SET @[email protected][email protected];
SELECT @sum;
# local variable
DECLARE m INT DEFAULT 1;
DECLARE n INT DEFAULT 1;
DECLARE SUM INT;
SET SUM=m+n;
SELECT SUM;
stored procedure
meaning : A set of precompiled SQL Collection of statements , Understand batch statements
1、 Improve code reuse
2、 Simplified operation
3、 Reduce the number of compilations and connections to the database server , Improved efficiency
Create Syntax
/*
1、 The parameter list consists of three parts
Parameter mode Parameter name Parameter type
give an example :
in stuname varchar(20)
Parameter mode :
in: This parameter can be used as input , That is, the parameter requires the caller to pass in a value
out: This parameter can be used as output , That is, the parameter can be used as the return value
inout: This parameter can be used as input or output , That is to say, the parameter needs to pass in the value , You can return a value
2、 If the stored procedure body has only one sentence ,begin end It can be omitted
Each of the stored procedure bodies sql A semicolon is required at the end of a statement .
The end of the stored procedure can use delimiter To reset
grammar :
delimiter End mark
Case study :
delimiter $
*/
CREATE PROCEDURE Stored procedure name ( parameter list )
BEGIN
Stored procedure body ( A combination of SQL sentence )
END
Call syntax
CALL Stored procedure name ( Argument list );
Use cases
1, Empty parameter list
SELECT * FROM admin;
CREATE PROCEDURE myp1()
BEGIN
INSERT INTO admin(username,`password`)
VALUES('john1','0000'),('lily','0000'),('rose','0000'),('jack','0000'),('tom','0000');
END
2, Create a in Stored procedure of mode parameters
CREATE PROCEDURE myp2(IN beautyName VARCHAR(20))
BEGIN
SELECT bo.*
FROM boys bo
RIGHT JOIN beauty b ON bo.id = b.boyfriend_id
WHERE b.name=beautyName;
END
# call
CALL myp2(' Small zhao ')
3. establish out Stored procedure of mode parameters
CREATE PROCEDURE myp6(IN beautyName VARCHAR(20),OUT boyName VARCHAR(20))
BEGIN
SELECT bo.boyname INTO boyname
FROM boys bo
RIGHT JOIN
beauty b ON b.boyfriend_id = bo.id
WHERE b.name=beautyName ;
SET @boyname
END
CALL myp6(' Small zhao ', @boyname)
SELECT @boyname
4. Create a inout Stored procedure of mode parameters
Pass in a and b Two values , Final a and b All double and return
CREATE PROCEDURE myp8(INOUT a INT ,INOUT b INT)
BEGIN
SET a=a*2;
SET b=b*2;
END
# call
SET @m=10
SET @n=20
CALL myp8(@m,@n)
SELECT @m,@n
Delete stored procedure
grammar :drop procedure Stored procedure name
DROP PROCEDURE p1
View stored procedures
grammar :SHOW CREATE PROCEDURE Stored procedure name
SHOW CREATE PROCEDURE myp2;
function
meaning : A set of precompiled SQL Collection of statements , Understand batch statements
1、 Improve code reuse
2、 Simplified operation
3、 Reduce the number of compilations and connections to the database server , Improved efficiency
difference :
stored procedure : There can be 0 Come back , There can also be multiple returns , Suitable for bulk insertion 、 Batch update
function : Yes and no 1 Come back , It is suitable to return a result after processing data
Create Syntax
CREATE FUNCTION Function name ( parameter list ) RETURNS Return type
BEGIN
The body of the function
END
Be careful :
1. parameter list It has two parts :
Parameter name Parameter type
2. The body of the function : There will be return sentence , If there's no mistake
If return The statement is not placed at the end of the function body and no error is reported , But not recommended
return value ;
3. There is only one sentence in the function body , You can omit begin end
4. Use delimiter Statement set end tag
Call syntax
SELECT Function name ( parameter list )
Use cases
1. Empty parameter list
# Case study : Number of employees returned to the company
CREATE FUNCTION myf1() RETURNS INT
BEGIN
DECLARE c INT DEFAULT 0;# Defining local variables
SELECT COUNT(*) INTO c# assignment
FROM employees;
RETURN c;
END
SELECT myf1()$
There are parameters and returns
#2. You can go back to
# Case study 1: According to the name of the employee , Return its wages
CREATE FUNCTION myf2(empName VARCHAR(20)) RETURNS DOUBLE
BEGIN
SET @sal=0;# Define user variables
SELECT salary INTO @sal # assignment
FROM employees
WHERE last_name = empName;
RETURN @sal;
END
# Case study 2: According to the Department name , Return the average wage of the Department
CREATE FUNCTION myf3(deptName VARCHAR(20)) RETURNS DOUBLE
BEGIN
DECLARE sal DOUBLE ;
SELECT AVG(salary) INTO sal
FROM employees e
JOIN departments d ON e.department_id = d.department_id
WHERE d.department_name=deptName;
RETURN sal;
END
View functions
grammar :SHOW CREATE FUNCTION Function name ;
SHOW CREATE FUNCTION myf3;
Delete function
grammar :
DROP FUNCTION Function name ;
DROP FUNCTION myf3;
Process control structure
Branching structure
if function
grammar :if( Conditions , value 1, value 2)
function : Implement two branches
Apply to begin end In or out
.case structure
grammar :
situation 1: Be similar to switch
case Variable or expression
when value 1 then sentence 1;
when value 2 then sentence 2;
…
else sentence n;
end
situation 2:
case
when Conditions 1 then sentence 1;
when Conditions 2 then sentence 2;
…
else sentence n;
end
Apply to begin end In or out
if structure
grammar :
if Conditions 1 then sentence 1;
elseif Conditions 2 then sentence 2;
…
else sentence n;
end if;
function : Similar to multiple if
It can only be applied to begin end in
The application case
Case study 1: Create a function , Achieve the results , If the result >90, return A, If the result >80, return B, If the result >60, return C, Otherwise return to D
CREATE FUNCTION test_if(score FLOAT) RETURNS CHAR
BEGIN
DECLARE ch CHAR DEFAULT 'A';
IF score>90 THEN SET ch='A';
ELSEIF score>80 THEN SET ch='B';
ELSEIF score>60 THEN SET ch='C';
ELSE SET ch='D';
END IF;
RETURN ch;
END
SELECT test_if(87)
Loop structure
while
grammar :【 label :】while The loop condition do
The loop body ;
end while【 label 】;
The application case
Circular insert
DROP PROCEDURE pro_while1
CREATE PROCEDURE pro_while1(IN insertCount INT)
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i<=insertCount DO
INSERT INTO admin(username,`password`) VALUES(CONCAT('Rose',i),'666');
SET i=i+1;
END WHILE;
END
边栏推荐
- [ta- frost wolf \u may- hundred people plan] 2.2 model and material space
- 171. excel table column No
- Class and object finalization
- [EI conference] 2022 international joint civil and Offshore Engineering Conference (jccme 2022)
- 浏览器顶部loading(来自知乎)
- 6. Z 字形变换
- Edge浏览器的小技巧:Enter+Ctrl可以自动将地址栏转换为网址
- NFT:使用 EIP-2981 开启 NFT 版税之旅
- 嵌入式系统开发笔记80:应用Qt Designer进行主界面设计
- Review column - message queue
猜你喜欢

Do280 management application deployment --rc

陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全
![[ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle](/img/94/99090ea91082a385968e071ef3766c.png)
[ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle

【TA-霜狼_may-《百人计划》】1.1 渲染流水线

NFT: start NFT royalty journey with eip-2981

Class and object finalization

Why can't you find the corresponding function by clicking go to definiton (super easy has a diagram)

Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products

Go learning --- unit test subtest
![[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions](/img/be/325f78dee744138a865c13d2c20475.png)
[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions
随机推荐
【TA-霜狼_may-《百人计划》】1.2.2 矩阵计算
熊市下的Coinbase:亏损、裁员、股价暴跌
Coinbase in a bear market: losses, layoffs, stock price plunges
【TA-霜狼_may-《百人计划》】1.2.1 向量基础
[ta - Frost Wolf May - 100 people plan] 2.3 Introduction aux fonctions communes
171. excel table column No
mysql 函数 变量 存储过程
Valentine's Day is nothing.
浏览器顶部loading(来自知乎)
LeetCode 1400. Construct K palindrome strings
Jeecgboot output log, how to use @slf4j
Use selenium automated test tool to climb the enrollment score line and ranking of colleges and universities related to the college entrance examination
【无标题】
Unity's 3D multi-point arrow navigation
Browser top loading (from Zhihu)
242. 有效的字母异位词
[ta- frost wolf \u may- hundred people plan] 1.1 rendering pipeline
高并发下接口幂等性如何保证?
All in one 1086: Jiaogu conjecture
[TA frost wolf \u may- hundred people plan] 1.3 secret of texture