-- 可以通过LIKE模糊匹配方式查找变量 SHOW [ SESSION | GLOBAL ] VARIABLES LIKE '......';
-- 查看指定变量的值 SELECT @@[SESSION | GLOBAL] 系统变量名;
设置系统变量
1 2
SET [ SESSION | GLOBAL ] 系统变量名 = 值 ; SET @@[SESSION | GLOBAL]系统变量名 = 值 ;
注意: 如果没有指定SESSION/GLOBAL,默认是SESSION,会话变量。 A. 全局变量(GLOBAL): 全局变量针对于所有的会话。 B. 会话变量(SESSION): 会话变量针对于单个会话,在另外一个会话窗口就不生效了。
演示示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
-- 查看系统变量 show session variables ; show session variables like 'auto%'; show global variables like 'auto%'; select @@global.autocommit; select @@session.autocommit;
-- 设置系统变量 set session autocommit = 1; -- 关闭自动提交后,如果重启mysql服务,还会变为开启状态 set global autocommit = 0;
SET 变量名 = 值 ; SET 变量名 := 值 ; SELECT 字段名 INTO 变量名 FROM 表名 ... ;
演示示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
-- 声明局部变量 - declare -- 赋值
drop procedure if exists p1; create procedure p1() begin declare user_total int default 0; select count(*) into user_total from tb_user; select user_total; end;
mysql> call p1(); +------------+ | user_total | +------------+ | 24 | +------------+ 1 row in set (0.01 sec)
if
介绍
if 用于做条件判断,具体的语法结构为:
1 2 3 4 5 6 7
IF 条件1 THEN ..... ELSEIF 条件2 THEN -- 可选 ..... ELSE -- 可选 ..... END IF;
在if条件判断的结构中,ELSE IF 结构可以有多个,也可以没有。 ELSE结构可以有,也可以没有。
drop procedure if exists p1; create procedure p1() begin declare salary int default 4999; declare result varchar(20); if salary>=10000 then set result := '高薪'; elseif salary >= 5000 and salary < 10000 then set result := '普通薪资'; else set result := '低薪'; end if; select result; end;
mysql> call p1(); +--------+ | result | +--------+ | 低薪 | +--------+ 1 row in set (0.00 sec)
drop procedure if exists p1; create procedure p1(in salary int, out result varchar(20)) begin if salary >= 10000 then set result := '高薪'; elseif salary >= 5000 and salary < 10000 then set result := '普通薪资'; else set result := '低薪'; end if; end;
mysql> select @salary; +---------+ | @salary | +---------+ | 8300 | +---------+ 1 row in set (0.00 sec)
case
介绍
case结构及作用,和我们在基础篇中所讲解的流程控制函数很类似。有两种语法格式:
语法1:
1 2 3 4 5 6
-- 含义: 当case_value的值为 when_value1时,执行statement_list1,当值为 when_value2时,执行statement_list2, 否则就执行 statement_list CASE case_value WHEN when_value1 THEN statement_list1 [ WHEN when_value2 THEN statement_list2] ... [ ELSE statement_list ] END CASE;
语法2:
1 2 3 4 5 6
-- 含义: 当条件search_condition1成立时,执行statement_list1,当条件search_condition2成立时,执行statement_list2, 否则就执行 statement_list CASE WHEN search_condition1 THEN statement_list1 [WHEN search_condition2 THEN statement_list2] ... [ELSE statement_list] END CASE;
drop procedure if exists p1; create procedure p1(in now_month int, out result varchar(20)) begin case when now_month>=1 and now_month<4 then set result := '第一季度'; when now_month >=4 and now_month<7 then set result := '第二季度'; when now_month >=7 and now_month<10 then set result := '第三季度'; when now_month >=10 and now_month <13 then set result := '第四季度'; else set result :='输入月份有误'; end case; end;
drop procedure if exists p1; create procedure p1(inout n int) begin declare total int default 0; while n>0 do set total := total + n; set n := n-1; end while; set n := total; end
mysql> set @r:=100; Query OK, 0 rows affected (0.00 sec)
drop procedure if exists p1; create procedure p1(in n int, out total int) begin set total := 0; repeat set total := total + n; set n := n - 1; until n <1 end repeat; end;
drop procedure if exists p1; create procedure p1(in n int, out res int) begin set res := 0 ; get_sum: loop if n<1 then leave get_sum; end if; set res := res+n; set n := n-1; end loop get_sum; end;
mysql> select @res; +------+ | @res | +------+ | 55 | +------+ 1 row in set (0.00 sec)
drop procedure if exists p1; create procedure p1(in n int, out total int) begin set total := 0; get_sum:loop if n<1 then leave get_sum; end if; if n%2=1 then set n := n - 1; iterate get_sum; end if; set total := total + n; set n := n - 1; end loop get_sum; end;
-- 逻辑: -- A. 声明游标, 存储查询结果集 -- B. 准备: 创建表结构 -- C. 开启游标 -- D. 获取游标中的记录 -- E. 插入数据到新表中 -- F. 关闭游标 create procedure p1(in uage int) begin declare uname varchar(100); declare upro varchar(100); declare u_cursor cursor for select name,profession from tb_user where age <= uage; drop table if exists tb_user_pro; create table if not exists tb_user_pro( id int primary key auto_increment, name varchar(100), profession varchar(100) ); open u_cursor; while true do fetch u_cursor into uname,upro; insert into tb_user_pro values (null, uname, upro); end while; close u_cursor; end;
-- 逻辑: -- A. 声明游标, 存储查询结果集 -- B. 准备: 创建表结构 -- C. 开启游标 -- D. 获取游标中的记录 -- E. 插入数据到新表中 -- F. 关闭游标 drop procedure if exists p1; create procedure p1(in user_age int) begin declare user_name varchar(30); declare user_profession varchar(30); declare user_cursor cursor for select name, profession from tb_user where age <= user_age; -- 声明条件处理程序 : 当SQL语句执行抛出的状态码为02000时,将关闭游标user_cursor,并退出 declare exit handler for sqlstate '02000' close user_cursor; drop table if exists tb_user_pro; create table tb_user_pro( id int primary key auto_increment, name varchar(30), profession varchar(30) ); open user_cursor; while true do fetch user_cursor into user_name, user_profession; insert into tb_user_pro(name, profession) values(user_name, user_profession); end while; close user_cursor; end
drop procedure if exists p1; create procedure p1(in user_age int) begin declare user_name varchar(30); declare user_profession varchar(30); declare user_cursor cursor for select name, profession from tb_user where age <= user_age; -- 声明条件处理程序 : 当SQL语句执行抛出的状态码为02开头时,将关闭游标user_cursor,并退出 declare exit handler for not found close u_cursor; drop table if exists tb_user_pro; create table tb_user_pro( id int primary key auto_increment, name varchar(30), profession varchar(30) ); open user_cursor; while true do fetch user_cursor into user_name, user_profession; insert into tb_user_pro(name, profession) values(user_name, user_profession); end while; close user_cursor; end