当前位置:网站首页>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系统的时代,已逐渐到来
- MES系统质量追溯功能,到底在追什么?
- El expression and JSTL
- 数据库基础——规范化、关系模式
- (P33-P35)lambda表达式语法,lambda表达式注意事项,lambda表达式本质
- In depth interpretation of 5g key technologies
- 从AC5到AC6转型之路(1)——补救和准备
- CMAKE 里PRIVATE、PUBLIC、INTERFACE属性示例详解
- . net mysql Too many connections
- Hands on learning and deep learning -- a brief introduction to softmax regression
猜你喜欢

Model Trick | CVPR 2022 Oral - Stochastic Backpropagation A Memory Efficient Strategy

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

Transformation from AC5 to AC6 (1) - remedy and preparation

PPP agreement

如何理解APS系统的生产排程?

Instructions spéciales pour l'utilisation du mode nat dans les machines virtuelles VM

DUF:Deep Video Super-Resolution Network Using Dynamic Upsampling Filters ...阅读笔记

ctfshow web 1-2

You get download the installation and use of artifact

Hands on deep learning 18 -- model selection + over fitting and under fitting and code implementation
随机推荐
Hands on learning and deep learning -- simple implementation of softmax regression
Ceres optimizer usage (self use)
Hands on learning and deep learning -- Realization of linear regression from scratch
DUF:Deep Video Super-Resolution Network Using Dynamic Upsampling Filters ... Reading notes
工厂的生产效益,MES系统如何提供?
MYSQL中的锁的机制
ctfshow web4
Database connection pool and dbutils tool
JSP technology
记录谷粒商城踩坑(一)
. net mysql Too many connections
ASP. Net project development practice introduction_ Item VI_ Error report (summary of difficult problems when writing the project)
Talk about the four basic concepts of database system
(p25-p26) three details of non range based for loop and range based for loop
Upgrade eigen to version 3.3.5 under Ubuntu 16.04
(p33-p35) lambda expression syntax, precautions for lambda expression, essence of lambda expression
Convolutional neural network CNN based cat dog battle picture classification (tf2.1 py3.6)
(P19-P20)委托构造函数(代理构造函数)和继承构造函数(使用using)
MES系统质量追溯功能,到底在追什么?
牛客网的项目梳理