数据库流程控制

2021/9/6 19:10:26

本文主要是介绍数据库流程控制,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一.顺序控制

顺序结构关键字:begin……end ,相当于{……},是SQL语言中的复合语句(语句块)。
顺序结构基本格式:

begin
	sql代码1; 
	sql代码2; 
	……
	sql代码n; 
end;

二.数据库变量

1.局部变量

-- 定义局部变量的基本格式
declare 变量名 数据类型;

-- 变量的初始化赋值
declare 变量名 数据类型 default 初始值;

-- 给变量赋值
set 变量名=变量值;

注意:局部变量只能在begin……end之间使用

delimiter // 
create procedure sum1(in x int,in y int) 
begin 
	declare z int default 0; 
	-- 定义一个局部变量z,并初始化赋值为0 
	set z = x+y; 
	select z as '两个数的和为'; 
end// 
delimiter ; 
call sum1(123,456); 
select z; -- 局部变量只能在其定义的begin……end之间使用

2.用户变量

-- 使用用户变量的一般格式:
set @变量名 = 初始值;
set @a = 10; 
select @a; 
set @a = 'abc'; 
select @a; 
set @b = '2000-1-1'; 
select @b;

从上面也能看出,用户变量是弱类型,没有指定的数据类型,可以支持多种数据类型,可以理解为万用类型
用户变量只能够在当前连接中使用,不同的数据库连接无法共用同一个数据库变量,变量名相同也无法共用。

用户变量应用举例

delimiter // 
create procedure sum2(in x int,in y int,out z int) 
-- 定义了一个用于传出数据的局 部变量z(形参) 
begin 
	set z = x+y; 
end// 
delimiter ; 
set @a = 11; 
set @b = 22; 
call sum2(@a,@b,@c); 
select @c; -- 通过全局变量带回存储过程中的运算结果

3.会话变量

系统自带的,是用来保存数据库当前会话的默认数据,当前连接有效,重新连接会恢复默认值。

-- 使用会话变量的基本格式:@@会话变量名
-- 查看当前连接的会话变量:
show session variables;

4.全局变量

系统自带的,是用来保存数据库当前服务的默认数据,当前服务有效,重启服务后恢复默认值。

-- 使用全局变量的基本格式: @@全局变量名
-- 查看当前服务的全局变量:
show global variables; 

三.分支选择结构

1.if else 结构

1.1 if语句的基本格式

if <判断条件> then <语句;> 
end if;

判断if之后的条件,如果条件满足则执行语句,否则不执行语句。(注意结束if需要用end if)

1.2 if else语句的基本格式

if <判断条件> then <语句1;> 
else <语句2;> 
end if;

判断if之后的条件,如果条件满足,则执行语句1,否则执行语句2。

1.3if else嵌套的基本格式

if <判断条件1> then <语句1;> 
elseif <判断条件2> then<语句2;> 
[elseif … then …;] 
[else 语句n;] 
end if;

判断if之后的条件1,如果条件1满足,则执行语句1,否则判断elseif之后的条件2,如果条件2满足则执行语句2,否则执行else之后的语句n。

例如:判断x是否大于100

delimiter // create procedure test1(in x int) 
begin 
	if (x>100) then select 'x大于100'; 
	-- if之后的条件表达式可以没有括号,括号知识用于改变优先级 
	elseif x=100 then select 'x等于100'; 
	else select 'x小于100'; 
	end if; -- if必须有end if;结尾 
end// 
delimiter ; 
call test1(99); 
call test1(100); 
call test1(111);

2.case多分支结构

-- case when 多分支结构基本格式:
case 条件 
	when 值1 then 语句1; 
	when 值2 then 语句2; 
	……
	when 值n then 语句n; 
	else 语句n+1; # 相当于default 
end case;

判断case之后的条件,如果条件值于其中某个when之后的值相等,则执行该when中then之后的语
句,如果都不满足则执行else之后的语句。相当于C语言中的switch case语句。

例如:对成绩(0~100)进行评分(优秀>=90、良好>=80、及格>=60、不及格<60)

delimiter // 
create procedure test2(in score int) 
begin 
	if score>100 && score<0 
		then select '成绩输入有误!';
	else case floor(score/10) # 取整函数floor, score div 10 -- 整除 
        when 10 then select '满分' as 成绩等级; 
        when 9 then select '优秀' as 成绩等级; 
        when 8 then select '良好' 成绩等级; 
        when 7 then select '及格' 成绩等级; 
        when 6 then select '及格' 成绩等级; 
        else select '不及格' 成绩等级; 
	end case; 
	end if; 
end// 
delimiter ; 
call test2(100); 
call test2(99); 
call test2(88); 
call test2(78); 
call test2(66); 
call test2(10);

四.循环结构

1.while…do循环结构

-- while循环基本格式:
while 条件表达式 
do 循环体语句; 
end while;

如何while之后的条件满足则循环执行do之后的循环体语句,直到条件不满足则结束循环。数据库中的while循环和C语言中的while循环一样

例如:求数字1到10的和

delimiter // 
create procedure tset3(out s1 int) 
begin 
	declare i int default 1; 
	declare s int default 0; 
	while i<=10 
	do
		set s = s+i; 
		-- SQL语言没有+=、-=、++、--、==等运算符 
		set i = i+1; 
	end while; 
	set s1 = s; 
end// 
delimiter ; 
call test3(@sum);
select @sum;

2.loop循环

2.1 loop循环基本格式

loop 循环体语句; 
end loop;

注意:这里的loop循环是一个无限循环(死循环),其没有结束条件,所以需要手动添加结束条件。

2.2 给loop循环添加结束条件

循环别名:loop 循环体语句; 
if 条件表达式 then leave 循环别名; end if; 
end loop;

首先执行loop之后的循环体语句,循环语句中的if条件满足则离开循环,否则继续循环。
这里给循环取别名,通过if语句判断结束条件,leave离开跳出循环(相当于C语言中的break)。

例如:求整数1到n的和

delimiter // 
create procedure test4(out s1 int,in n int) 
begin 
	declare i int default 1; 
	declare s int default 0; 
	sums:loop 
		set s = s+i; 
		set i = i+1; 
		if i > n 
			then leave sums; 
		end if; 
	end loop; 
	set s1 = s; 
end// 
delimiter ; 
call test4(@sum,10); 
select @sum; 
call test4(@sum,100);

3.repeat循环

-- repeat循环的基本格式:
repeat 循环体语句; 
until 条件表达式 
end repeat;

首先执行repeat之后的循环体语句,然后判断until之后的条件,如果条件不满足则继续循环,否则条件满足则结束循环。

注意:repeat循环相当于C语言中的do while循环,都是先执行一次循环体再进行条件判断,但是不同的是,do while循环是条件不满足时才结束循环,而repeat是条件满足时才结束循环。并且until子句后不能有’;'分号。

例如:求整数n到m的和

delimiter // 
create procedure test5(out s1 int,in n int,in m int) 
begin 
	declare k int default n; 
	if n>m 
		then set n = m; 
		set m = k; 
	end if; 
	declare i int default n; 
	declare j int default n+1; 
	repeat 
		set i = i+j; 
		set j = j+1; 
	until j>m -- 如果j>m则离开repeat循环 
	end repeat; 
	set s1 = i; 
end// 
delimiter ; 
set @x = 1; 
set @y = 10; 
call test5(@sum,@x,@y); 
select @sum; 
set @x = 5; 
set @y = 16;


这篇关于数据库流程控制的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程