1.写一个完整的包括参数、变量、变量赋值、条件判断、UPDATE语句、SELECT返回结果集的存储过程。
CREATE table zy3
(
id int,
name varchar(11),
at int
);
insert into zy3 values(1,'haha',3),(2,'hehe',2),(3,'hoho',1);
select * from zy3;
delimiter //
create procedure a (in at int,in name varchar(11),out id int)
begin
update a set at = at where name = name;
select id into id from a where name = name;
end //
delimiter ;
call a (1,'haha',@id);
select @id;
2.创建一个执行动态SQL的存储过程。
create table students
(
id int,
name varchar(11),
class int
);
insert into students values(1,'zs',2),(2,'ls',1),(3,'ww',3);
select * from students;
delimiter //
create procedure name_class (in nm varchar(11),out bj int)
begin
if nm not in ('zs','ls','ww')
then select 'not found';
update students set class = 1 where name = 'zs';
else select class into bj from students where name = nm;
end if;
end //
delimiter ;
call name_class ('zs',@bj);
select @bj;
call name_class ('xxx',@bj);
select class from students where name = 'zs';
3.创建实现功能相同的存储函数,比较他们之间的不同点在什么地方?
1.存储函数有且只有一个返回值,而存储过程可以有多个返回值,也可以没有返回值。
2.存储函数只能接受输入参数,而存储过程可以接受输入、输出、输入输出参数。
3.存储过程可以调用存储函数、但函数不能调用存储过程。
4.存储过程中的语句功能更强大,存储过程可以实现很复杂的业务逻辑,而函数有很多限制,如不能在函数中使用insert,update,delete,create等语句;
5.存储过程一般是作为一个独立的部分来执行(call调用)。而函数可以作为查询语句的一个部分来调用。
6.存储函数只完成查询的工作,可接受输入参数并返回一个结果,也就是函数实现的功能针对性比较强。