#存储过程中的变量:1.系统变量 2.用户自定义变量 #系统变量:MySQL服务器提供,不是用户定义的,属于服务器层面。分为全局变量(GLOBA;)和会话变量(SESSION) #全局变量在所有会话中都生效,会话变量只在当前会话中生效 #查看系统变量: # show session variables ; # show global variables like '...'; # select @@session/global 系统变量名 #默认是会话级别的系统变量 # select @@autocommit;查看指定变量状态 #设置系统变量 # set session autocommit = 0; # set global autocommit = 0 ; #设定了全局变量之后重启全局变量仍然会进行初始化 #用户定义变量:用户根据需要自己定义的变量。不用提前声明,直接使用“@变量名”即可。其作用域为当前连接 #作用域为当前连接 #用户定义变量赋值 # set @var_name = expr; # set @var_name := expr; # select @var_name := expr; # select 字段 into @var_name from table_1; set @myname = 'xm'; set @myage := 10; set @mygender := 'm',@myhobby := 'python'; select @myname,@myage,@mygender,@myhobby; select @mycolor := 'red'; select count(*) into @mycount from tb_user; #用户自定义变量不需要进行声明或者初始化,只不过没赋值的用户变量读取到的是null #局部变量:根据需要定义的在局部生效的变量,访问前需要declare声明,可以用作存储过程内的局部变量和输入参数 #生效范围是在其内声明的begin..end块 #变量声明 # declare 变量名 变量类型; #变量类型就是数据库字段类型:int,bigint,char,varchar,date,time等 #变量赋值 # set var_name = expr; # set var_name := expr; # select 字段名 into 变量名 from 表名; # create procedure p2() # begin # declare stu_count int default 0; # set stu_count = 100; # set stu_count := 100; # select (*) into stu_count from student; # end; #if判断 #if 条件1 then #.... #elseif 条件2 then #... #else #... #end if; create procedure p3() begin declare score int default 58; declare result varchar(10); if score >= 85 then set result := '优秀'; elseif score >= 60 then set result := '及格'; else set result := '不及格'; end if; select result; end; call p3(); create database test; use test; #参数类型 #in:作为输入参数,调用时传入,默认类型 #out:作为输出参数,可以作为返回值 #inout:既可以作为输入参数,也可以作为输出参数 # create procedure p4(in/out/inout 参数名 参数类型) # create procedure p4(in score int,out result varchar(10)) # begin # if score >= 85 then # set result = '优秀'; # elseif score >= 60 then # set result = '及格'; # else # set result := '不及格'; # end if; # end; # # call p4(15,@result);随便选择一个用户定义变量 # select @result; # create procedure p5(inout score double) # begin # set score := score*0.5; # # end; # # set @score = 148; # call p5(@score); # select @score; #inout类型不能直接写入数据传入传出,因为输入值也要进行返回,相当于是一个变量 #case #用法一: # case case_value # when when_value1 then statement_list1 # when when_value2 then statement_list2 # else statement_list # end case; #用法二: # case # when condition1 then statement_list1 # when condition2 then statement_list2 # else statement_list # end case; # create procedure p7(in month int,out result varchar(10)) # begin # case # when month>=1 and month<=3 then set result = '第一季度'; # when month>=4 and month<=6 then set result = '第二季度'; # when month>=7 and month<=9 then set result = '第三季度'; # when month>=10 and month<=12 then set result = '第四季度'; # end case ; # select concat(month,result); # end; # call p7(4,@result); #while:有条件的循环控制语句 # while 条件 do # sql语句 # end while create procedure p8(in n int) begin declare total int default 0; while n>0 do set total = total+n; set n = n-1; end while; select total; end; call p8(5); #repeat:有条件的循环控制语句,当满足条件的时候退出循环 # repeat # sql语句 # until condition # end repeat; create procedure p10(n int) begin declare total int default 0; repeat set total = total + n; set n = n-1; until n=0 end repeat; select total; end; call p10(5); #loop:如果不增加退出循环的条件可以实现简单的死循环,配合两个语句使用 #leave:配合循环使用,退出循环 #iterate:必须用在循环中,跳过当前循环剩下的语句,直接进入下一次循环 # [begin_lable:] loop # sql逻辑 # end loop[end_label] # leave lable;退出指定标记的循环体 # iterate lable;直接进入下一次循环 create procedure p11(n int) begin declare total int default 0; sum:loop if n<= 0 then leave sum; end if; set total = total + n; set n = n - 1; end loop sum; select total; end; call p11(5); create procedure p11(n int) begin declare total int default 0; sum:loop if n<= 0 then leave sum; end if; if n%2 = 1 then set n = n-1; iterate sum; end if; set total = total + n; set n = n - 1; end loop sum; select total; end; #游标/光标cursor:用来存储查询结果集的数据类型,在存储过程和函数中可以使用游标对结果集进行循环的处理 #声明游标 #declare 游标名称 cursor for 查询语句; #打开游标 # open 游标名称; #获取游标记录 # fetch 游标名称 into 变量; #关闭游标 # close 游标名称 create procedure p12(uage int) begin declare uname varchar(10); declare upro varchar(100); declare age_cs cursor for select name,profession from tb_user where age < uage; create table tb_user_pro( id int primary key auto_increment, name varchar(10), profession varchar(100) ); open age_cs; while true do fetch age_cs into uname,upro; insert into tb_user_pro values (null,uname,upro); end while; close age_cs; end; #1.其他变量的声明要在游标前 #2.条件设置
MySQL学习记录day9
最新推荐文章于 2024-07-22 17:39:53 发布