存储过程中的异常处理:

 

 

 

 之前定义了一些存储过程也搬过来用用:

/*get_word过程*/

delimiter $$
drop procedure if exists get_word;
create procedure get_word(in s char(100),out w char(1))
begin
declare len tinyint;
set len = char_length(s);
set @a = floor(rand()*(len+1)+1);
set w = substring(s,@a,1);
end $$
delimiter ;

/*no.1 以下是一个过程get_a_character的部分代码,需要大家完善代码。该过程实现输入任意一个字符串(s),该过程执行后从s字符串中任意抽取一个字符,并将此字符返回给变量w.可以通过以下语句测试get_word过程是否编写成功.*/

delimiter $$
drop procedure if exists get_a_character;
create procedure get_a_character(in s char(100),out w char(1))
begin
call get_word(s,w);
end $$
delimiter ;
set @x = '刘王张赵李钱朱何孙曾陈吴黄刘邓周毛江胡';

call get_a_character(@x,@k);
select @k;

/*no.2 编写存储过程get_name,该过程根据cd变量的值判断名字是1字还是2字,再根据xb的值判断m是取m2(男)的值还是取m1(女)的值*/

delimiter $$
drop procedure if exists get_name;
create procedure get_name(out m varchar(2),out xb int)
begin
declare cd int;
declare ms1,ms2 varchar(50);
declare m1,m2,m3 varchar(2);
set ms1='依秋香巧紫萱莉玉碧丽念雅红燕艳莲荷蕾紫莹颖琪';
set ms2='澄德海超阳昌瀚亮锋涵煦杰俊诚毅峰衍浩广邈言博畅';
set xb = floor(rand()*2);
set cd = floor(rand()*2+1);
if xb = 0 then
call get_word(ms1,m1);
call get_word(ms1,m2);
set m3 = concat(m1,m2);
elseif xb = 1 then
call get_word(ms2,m1);
call get_word(ms2,m2);
set m3 = concat(m1,m2);
end if;
if cd = 1 then set m = m1;
elseif cd = 2 then set m = m3;
end if;
end $$
delimiter ;
call get_name(@name,@sex);
select @name,@sex;

/*no.3 main2 过程*/

delimiter $$
drop procedure if exists main2;
create procedure main2()
begin
declare x varchar(100);
declare xing,ming,xm varchar(3);
declare sex,n,f int;
declare xh varchar(8);
declare continue handler for sqlexception set f=2;
drop table if exists xs2;
create table xs2 as select 学号,姓名,性别 from xs limit 0 ;
create unique index index_name on xs2(姓名);
set xh = '191100';
while xh<='191999' do
call get_a_character(@x,@k);
set xing = @k;
call get_name(@name,@sex);
set ming = @name;
set sex = @sex;
set xm = concat(xing,ming);
insert into xs2 values(xh,xm,sex);
if f=2 then
select xh,xm,'重名了';
set f=1;
elseif f=1 then
set xh = xh+1;
end if;
end while;
end $$
delimiter ;
call main2();
select * from xs2;

 

下面是一个网址可以选择接着问问百度ai这些相应的问题,这个ai很强:

https://www.baidu.com/s?ie=UTF-8&wd=linux%E6%80%8E%E4%B9%88%E7%A7%BB%E5%8A%A8%E6%96%87%E4%BB%B6%E5%A4%B9