存储过程in参数实例:
delimiter $$;
create procedure test1(id int,phone int)
begin
declare s_name varchar(12);
declare sex varchar(10) default '男';
set id = id+1;
set s_name = '邓肯';
insert into students values(id,s_name,phone,sex);
end
$$;
delimiter;
set @id=98;
call test1(@id,8232342);
select * from students;
select @id;
存储过程out参数实例:
delimiter $$;
create procedure test2(id int,out phone int)
begin
declare s_name varchar(12);
declare sex varchar(10) default '男';
set id = id+1;
Select phone;
set phone = 186125312;
set s_name = '邓肯';
insert into students values(id,s_name,phone,sex);
end
$$;
delimiter;
set @phone=99888;
call test2(68,@phone);
select * from students;
select @phone;
存储过程inout参数实例:
delimiter $$;
create procedure test3(id int,out phone int,inout s_name varchar(20))
begin
declare sex varchar(10) default '男';
set id = id+1;
set phone = 186125312;
select s_name;
set s_name = 'DBA';
insert into students values(id,s_name,phone,sex);
end
$$;
delimiter;
set @phone=99888;
set @s_name='dba'
call test3(70,@phone,@s_name);
select * from students;
select @s_name;
存储过程中的if条件判断语句格式:
if 条件 then
语句
elseif 条件 then
语句
else
语句
end if;
存储过程if判断实例:
现在有一个besttest表,里面有班级和学生id,来写一存储过程实现插入小于100的学号班级是以前班,大于等于100的是乔巴,大于等于200的是索隆班
create table besttest(s_no int,class varchar(20)); # 创建表
delimiter $$;
create procedure test4(s_no int)
begin
declare class_name varchar(10);
if s_no < 100 then
set class_name='以前班';
elseif s_no>=100 and s_no<200 then
set class_name='乔巴班';
else
set class_name='索隆班';
end if;
insert into besttest values(s_no,class_name);
end
$$;
delimiter;
call test4(100);
存储过程中的case条件判断语句格式:
case value
when 条件 then
sql语句
when 条件2 then
sql语句
else#如果上面条件都不满足的话执行
sql语句
end case
存储过程case判断实例:
现在有一个orders表,里面有id和订单状态,0代表未支付,1代表已支付,2代表已发货,3代表已收货,4代表已完成,其他值代表未知状态
create table orders(id int, status varchar(20));
delimiter $$;
create procedure test5(order_id int)
begin
declare status varchar(20);
case oder_id
when 0 then
set status='未支付';
when 1 then
set status='已付款';
when 2 then
set status='已发货';
when 3 then
set status='已收货';
when 4 then
set status='已完成';
else
set status='未知';
end case;
insert into orders values(order_id,status);
end
$$;
delimiter;
call test5(1, '已付款');
存储过程中的while循环(经常使用while来造数据):
while 条件 do
sql语句
end while
while循环实例:
delimiter $$;
create procedure test6(count int)
begin
declare name varchar(20);
declare sex varchar(10);
declare phone int(20);
declare i int;
set i = 0;
set sex='男';
while i<count do
set name=concat('帕克',i);
set phone=18612545 + i;
insert into students(name,phone,sex) values(name,phone,sex);
set i=i+1;
end while;
end
$$;
delimiter;
call test6(500);
select count(*) from students;
存储过程中的repeat循环:
repeat和while一样,也是一种循环的语句,和while不一样的是,repeat循环至少会执行一次,repeat语句格式:
repeat
sql语句
until 条件
end repeat;
repeat循环实例:
delimiter $$;
create procedure test7(count int)
begin
declare name varchar(20);
declare sex varchar(10);
declare phone int(20);
declare i int;
set i = 0;
set sex='男';
repeat
set name=concat('big',i);
set phone=18612545 + i;
insert into students(name,phone,sex) values(name,phone,sex);
set i=i+1;
until i>count
end repeat;
end
$$;
delimiter;
call test7(500);
select count(*) from students;