当前位置:网站首页>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
边栏推荐
猜你喜欢
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
[TA frost wolf \u may- hundred people plan] 1.3 secret of texture
It's settled! 2022 JD cloud summit of JD global technology Explorer conference see you in Beijing on July 13
Why can't you find the corresponding function by clicking go to definiton (super easy has a diagram)
Millet College wechat scanning code login process record and bug resolution
不同性能测试工具的并发模式
Recommend the best product development process in the Internet industry!
京东智能客服言犀意图体系搭建和意图识别技术介绍
熊市下的Coinbase:亏损、裁员、股价暴跌
NFT: start NFT royalty journey with eip-2981
随机推荐
Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security
LeetCode 1828. Count the number of points in a circle
分账技术赋能农贸市场,重塑交易管理服务效能
[EI search] important information conference of the 6th International Conference on materials engineering and advanced manufacturing technology (meamt 2022) in 2022 website: www.meamt Org meeting time
171. Excel 表列序号
208. implement trie (prefix tree)
10. regular expression matching
283.移动零
[JPCs publication] the Third International Conference on control theory and application in 2022 (icocta 2022)
206.反转链表
【TA-霜狼_may-《百人计划》】1.2.3 MVP矩阵运算
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
TS type gymnastics: illustrating a complex advanced type
392. judgment subsequence
Libevent Library Learning
Unity's 3D multi-point arrow navigation
171. excel table column No
LeetCode 1399. Count the maximum number of groups
MFC window scroll bar usage
[ta - Frost Wolf May - 100 people plan] 2.3 Introduction aux fonctions communes