当前位置:网站首页>Calling stored procedures in mysql, definition of variables,
Calling stored procedures in mysql, definition of variables,
2022-06-12 08:16:00 【Eldest brother Li dada】
1. Calling stored procedure
1. Its essence is to put some sql The process of encapsulating statements into fixed blocks of code is similar to c Functions in language .
for example : The following is to encapsulate into fixed code blocks
create procedure proc01()
begin
select empno,ename from emp;
end $$
delimiter;
2. Calling stored procedure :
call Defined process name
for example :
call proc01();
2. Statement / Defining variables ( User variables , Global variables , Session variables )
1. Definition / Declare variables ;
declare var_name01 varchar(20) default 'aaa';
Using stored procedures to change declared variables
delimiter $$
create procedure proc02()
begin
declare var_name01 varchar(20) default 'aaa'; -- Statement / Defining variables
set var_name01 = 'shangsan';
select var_name01;
end $$
delimiter;
call proc02();
2. Define user variables ;
@var_name01;
for example :
delimiter $$
create procedure proc04()
begin
set @var_name01 = ' Beijing ';
end $$
delimiter;
call proc04();
select @var_name01;
3. Define global variables :
@@global.var_name
-- View global variables
show global variables;
-- View a local variable
select @@global.auto_increment_increment;
-- Modify global variables
set global sort_buff_size = 50000;
set @@global.sort_buff_size = 50000;
4. Define session variables
The usage is similar to global variables and user variables
@@session.var_name
Works only in the current session
3. keyword in,out,inout Use
in
delimiter $$
create procedure proc06(in param_empno int)
begin
select * from emp where empno = param_empno;
end $$
delimiter;
call proc06(1001);
delimiter $$
create procedure proc07(in dname varchar(50), in sal decimal(7,2))
begin
select * from dept a,emp b where a.deptno = b.deptno and a.dname = dname and b.sal = sal;
end $$
delimiter;
call proc07(' Department of science and Engineering ',20000);
out keyword :
delimite $$
create procedure proc08(in in_empno int,out out_name varchar(50))
begin
select ename into out_ename from emp where empno = in_empno;
end $$
delimiter;
call proc08(1001,@o_ename);
select @o_ename;
inout keyword :
-- inout
delimiter $$
create procedure proc10(inout num int)
begin
set num = num * 10;
end $$
delimiter ;
set @inout_num = 3;
call proc10(@inout_num);
select @inout_num;
-- Incoming employee name , Splicing department number , Transfer in salary , Find out the annual salary
delimiter $$
create procedure proc11(inout ename varchar(50),inout sal int)
begin
select concat_ws('_',deptno,ename) from emp where emp.ename = ename;
set sal = sal * 12;
end $$
delimiter;
set @inout_name=' Guan yu ';
set @inout_sal = 3000;
call proc11(@inout_name,@inout_sal);
select @inout_sal;
select@inout_name;
4. Judgment in stored procedures
-- control - Judge
-- grammar
/*[elseif search condition 2 then statement list 2] [else statement list n] end if*/
-- Case study 1
-- Enter student's grade , To judge the level of achievement
delimiter $$
create procedure proc_12_if(in score int)
begin
if score <60
then
select ' fail, ';
elseif score>=60 and score<80
then
select' pass ';
elseif score>=80 and score<90
then
select ' good ';
elseif score>=90 and score<100
then
select ' good ';
elseif score>100
then
select ' error ';
end if;
end $$
delimiter ;
set @score = 55;
call proc_12_if(@score);
delimiter $$
create procedure proc_13_if(in in_name varchar(20))
begin
declare var_sal decimal(7,2);
declare result varchar(20);
seelct sal into var_sal from emp where ename = in_name;
if var_sal<10000
then
set result = ' Probationary salary ';
elseif var_cal < 20000
then
set result = ' Salary for regular employment ';
else
set result = ' Veteran salary ';
end if;
end $$
delimiter ;
-- call :
call proc_13_if(' Guan yu ')
-- Flow control statement :case
delimiter $$
create procedure proc14_case( in pay_type int)
begin
case pay_type
when 1 then select' Wechat payment ';
when 2 then select' Alipay pay ';
when 3 then select' Bank card payment ';
else select ' Other payment methods ';
end case;
end $$
delimiter;
call proc14_case(2);
-- Format 2
delimiter $$
create procedure proc_14_case(in score int)
begin
case
when score <60
then
select ' fail, ';
when score>=60 and score<80
then
select' pass ';
when score>=80 and score<90
then
select ' good ';
when score>=90 and score<100
then
select ' good ';
when score>100
then
select ' error ';
end case;
end $$
delimiter ;
5. Loops in stored procedures :
-- loop :
create table user(
uid int primary key,
username varchar(50),
password varchar(50)
);
delimiter $$
create procedure proc16_while(in insertCount int)
begin
declare i int default 1;
lable: while i<=insertCount do
insert into user(uid,username,password) values (i,concat('user-',i),'123456');
set i = i+1;
end while lable;
end $$
delimiter;
call proc17_while_leave(10);
-- while+leave
truncate table user;
delimiter $$
create procedure proc17_while_leave(in insertCount int)
begin
declare i int default 1;
lable: while i<=insertCount do
insert into user(uid,username,password) values (i,concat('user-',i),'123456');
if i = 5 then
leave lable;
end if;
set i = i+1;
end while lable;
end $$
delimiter;
-- while + iterate Skip this cycle , Make the next cycle
delimiter $$
create procedure proc18_while_iterate(in insertCount int)
begin
declare i int default 1;
lable: while i<=insertCount do
insert into user(uid,username,password) values (i,concat('user-',i),'123456');
set i = i+1;
if i = 5 then
iterate lable;
end if;
end while lable;
end $$
delimiter;
-- repeat
delimiter $$
create procedure proc18_repeat(in insertCount int)
begin
declare i int default 1;
lable: repeat
insert into user(uid,username,password) values (i,concat('user-',i),'123456');
set i = i+1;
until i>insertCount
end repeat lable;
end $$
delimiter;
call proc18_repeat(10);
-- loop loop
delimiter $$
create procedure proc19_loop(in insertCount int)
begin
declare i int default 1;
label:loop
insert into user(uid,username,password) values (i,concat('user-',i),'123456');
set i = i+1;
if i>insertCount
then
leave label;
end if;
end loop label;
end $$
delimiter;
call proc19_loop(10);
6. Cursors in stored procedures
-- The cursor (cursor)
-- declare cursor
-- Open cursor
-- Get value through cursor
-- Close cursor
delimiter $$
create procedure proc_18_cursor(in in_dname varchar(50))
begin
-- Defining local variables
declare var_empno int;
declare var_ename varchar(50);
declare var_sal decimal(7,2);
-- declare cursor
declare my_cursor cursor for
select empno,ename,sal
from dept a, emp b
where a.deptno = b.deptno and a.dname = in_dname;
-- Open cursor
open my_cursor;
-- Get value through cursor
label: loop
fetch my_cursor into var_empno,var_ename,var_sal;
select var_empno,var_ename,var_sal;
end loop label;
-- Close cursor
close my_cursor;
end $$
delimiter;
call proc19_cursor();
7. Exception handling in storage structures :
delimiter $$
create procedure proc_21_cursor_handler(in in_dname varchar(50))
begin
-- Defining local variables
declare var_empno int;
declare var_ename varchar(50);
declare var_sal decimal(7,2);
-- Define tag values :
declare flag int default 1;
-- declare cursor
declare my_cursor cursor for
select empno,ename,sal
from dept a, emp b
where a.deptno = b.deptno and a.dname = in_dname;
-- Define handle : Define how to handle exceptions
/* 1. How the program should execute after exception handling continue : Continue with the remaining code exit : Directly terminate the program 2. The trigger condition : Condition code : Condition name : SQLWARNING NOT FOUND SQLEXCEPTION 3. What code is executed after the exception is triggered : Set up flag Value --- > 0 */
declare continue handler for 1329 set flag = 0;
-- Open cursor
open my_cursor;
-- Get value through cursor
label: loop
fetch my_cursor into var_empno,var_ename,var_sal;
--
if flag = 1 then
select var_empno,var_ename,var_sal;
else
leave label;
end loop label;
-- Close cursor
close my_cursor;
end $$
delimiter;
call proc21_cursor();
边栏推荐
- 目前MES应用很多,为什么APS排程系统很少,原因何在?
- Introduction to SDI video data stream format (frequency, rate, YUV, EAV, SAV)
- Gtest/gmock introduction and Practice
- Upgrade eigen to version 3.3.5 under Ubuntu 16.04
- Cookies and sessions
- (P14)overrid关键字的使用
- OpenMP task 原理與實例
- What kind of sparks will be generated when the remote sensing satellite meets the Beidou navigation satellite?
- 智能制造的时代,企业如何进行数字化转型
- Beidou satellite navigation system foundation part 1
猜你喜欢

ctfshow web4

Vision transformer | arXiv 2205 - TRT vit vision transformer for tensorrt

MATLAB image processing -- image transformation correction second-order fitting

Servlet

Hands on deep learning -- activation function and code implementation of multi-layer perceptron

(P40-P41)move资源的转移、forward完美转发

Prediction of COVID-19 by RNN network

数据库基础——规范化、关系模式

MinGW offline installation package (free, fool)

System service configuration service - detailed version
随机推荐
S-msckf/msckf-vio technical route and code details online blog summary
Learning notes (1): live broadcast by Dr. Lu Qi - face up to challenges and grasp entrepreneurial innovation opportunities - face up to challenges and grasp entrepreneurial innovation opportunities -1
PPP agreement
牛客网的项目梳理
企业为什么要实施MES?具体操作流程有哪些?
vscode 下载慢解决办法
企业上MES系统的驱动力来自哪里?选型又该注意哪些问题?
JSP technology
Transformation from AC5 to AC6 (1) - remedy and preparation
Figure neural network makes Google maps more intelligent
Vision Transformer | Arxiv 2205 - TRT-ViT 面向 TensorRT 的 Vision Transformer
Hands on deep learning -- implementation of multi-layer perceptron from scratch and its concise implementation
(P33-P35)lambda表达式语法,lambda表达式注意事项,lambda表达式本质
Derivation of Poisson distribution
离散 第一章
tmux常用命令
对企业来讲,MES设备管理究竟有何妙处?
uni-app用canvas截屏并且分享好友
Vision Transformer | Arxiv 2205 - LiTv2: Fast Vision Transformers with HiLo Attention
Uni app screenshot with canvas and share friends