一、写一个完整的包括参数、变量、变量赋值、条件判断、UPDATE语句、SELECT返回结果集的存储过程。
二、创建一个执行动态SQL的存储过程。
1.进入MySQL
2.查看fruits
3.查看数据库
4.进入zhuzhuxia数据库并查看表的数量
5.查看表的内容
6.创建存储过程
(1)参数
delimiter //
create procedure countproc1 (in sid int,out num int)
begin
select count(*) into num from fruits where s_id = sid;
end //
delimiter ;
(2)变量赋值
delimiter //
create procedure countproc2 (in sid int,out num int)
begin
declare cnt int default 0 ;
select count(*) into num from fruits where s_id = sid;
set cnt = 100 ;
select cnt ;
end //
delimiter ;
(3)条件判断
delimiter //
create procedure countproc3 (in sid int,out num int)
begin
declare cnt int default 0 ;
declare tmp int default 0 ;
select count(*) into num from fruits where s_id = sid;
set cnt = 100 ,tmp =0;
select cnt ;
set tmp = cnt + 1;
if tmp > 100 then
select tmp;
else
select cnt;
end if;
end //
delimiter ;
(4)UPDATE语句
delimiter //
create procedure countproc4 (in sid int,out num int)
begin
declare cnt int default 0 ;
declare tmp int default 0 ;
select count(*) into num from fruits where s_id = sid;
set cnt = 100 ,tmp =0;
select cnt ;
set tmp = cnt + 1;
if tmp > 100 then
select tmp;
else
select cnt;
end if;
update fruits set f_name = 'OK' where f_id = 'a' ;
select * from fruits;
end //
delimiter ;
(5)正常调用
call countproc1(101,@num);
删除 countproc3
drop procedure countproc3;
;
三、创建实现功能相同的存储函数,比较他们之间的不同点在什么地方?
1.存储过程是用来执行一系列的SQL语句并返回结果集,而存储函数是用来返回单一值;
2. 存储过程可以包含输入和输出参数,而存储函数只能包含输入参数;
3. 存储过程可以包含数据操作语言(DML)语句,比如INSERT、UPDATE、DELETE,而存储函数不能包含这些语句;
4. 存储函数可以在SELECT语句中使用,而存储过程不能直接在SELECT语句中使用。